Medo
Medo

Reputation: 967

C# can't open html file

static void Main(string[] args)
{ 
   var x = File.Exists(@"C:\Users\user\Desktop\page_1.html");
   var y = File.ReadAllText(@"C:\Users\user\Desktop\page_1.html");
   var z = new FileInfo(@"C:\Users\user\Desktop\page_1.html");    
} 

I debug my code and add my variables to watch and it says "The name 'x' does not exist in the current context" and so on for other two. I know the file exists because if I change name to something else it will throw file not found.

Why is this happening? Is it a stupid mistake I'm not seeing or could something be wrong with the file?

Upvotes: 0

Views: 154

Answers (2)

Medo
Medo

Reputation: 967

Console.Write(x);

When I added that line x was finally visible in watch. But still no idea why it was needed.

Upvotes: 0

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

Why don't you try like this :

string filePath=@"C:\Users\user\Desktop\page_1.html"
if(File.Exists(filePath))
{
 // read your file
}
else
{
 Console.WriteLine("File Not Found");
}

Upvotes: 1

Related Questions