views:

99

answers:

2

Hi

I am trying to read a plain text file named "test.txt" from my system but all the attempts File.Exists(), StreamReder are not getting the file, that is not a new task for me but i am irritated due to this strange behavior. I have given full permissions to the file but in vain. I am doing the test in a C# console application. The system has a fresh installation and I am wondering of any permission issue when run in debug mode. I also copied the file to Debug folder but still same error. Can anyone please guide me about this? Thanks in advance

+3  A: 

There is a neat function in C# for reading string files: (in System.IO namespace)

string text = File.ReadAllText("test.txt");

If you are having trouble with the path, you can add test.txt as a resource with copy to (add the file to teh project, right-click properties and select Copy to output directory.

Then you can use:

string path = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
File.ReadAllText(path);
Russell
I would use `Path.Combine(Directory.GetCurrentDirectory(), "test.txt");` instead of string concatenation.
George Howarth
Thanks @George, that will help with the always dubious backslash suffix. :)
Russell
+1  A: 

Have you stepped through the code? The first step is to make sure the path being used in the program is correct.

Debug mode will still run under your account so, if you have permission to open the file, that won't be a problem.

Phil