C# File Reading Fails

Howard Bash
Howard Bash used Ask the Experts™
on
The following chunk of code "should work":
            string[] FilesToRead = { @"C:\mycode\datafiles\foo.txt" };

            int FileCount = FilesToRead.Length;
           
            for (int fPtr = 0; fPtr < FileCount; fPtr++)
            {
                string nextFile = FilesToRead[fPtr];

                bool b = File.Exists(nextFile);

                string[] lines = System.IO.File.ReadAllLines((nextFile);

            }

Open in new window


The folder and file is there on my C drive.  However,  I get a file not found exception.  I am on a Windows 10 Pro machine and using VS2013.

Is System.IO broken?

Screen Capture of the Environment During Exception
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try to see if you are able to validate with exists method.

string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");

Open in new window

Commented:
Also you can try to get fileinfo with this method. This will also give you another way if you are accessing correct or valid file.

// The name of the file.
	const string fileName = { @"C:\mycode\datafiles\foo.txt" };

// Create new FileInfo object and get the Length.
	FileInfo f = new FileInfo(fileName);
	long s1 = f.Length;

Open in new window

Commented:
In your junk code you can also add a condition..

 for (int fPtr = 0; fPtr < FileCount; fPtr++)
            {
                string nextFile = FilesToRead[fPtr];

                bool b = File.Exists(nextFile);

                if (b == True)
               {
                       string[] lines = System.IO.File.ReadAllLines((nextFile);
               }

            }

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Howard BashSenior Software Engineer

Author

Commented:
I created a c:\temp folder in it and put a text file named text.txt.

I executed this code:
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");

And File does not exist was written to console.
Howard BashSenior Software Engineer

Author

Commented:
Since File.Exists says no file,  getting it's length shouldn't work, no?

Commented:
Check if permission issue. Try to give full read and write access to temp folder.

Next which user account .net application using..check and try give access to temp folder.
Howard BashSenior Software Engineer

Author

Commented:
FileReadCrash2.jpgThis is my local machine.  I checked permissions on the temp folder (which I created for this test) and the folder gives users:
Karrtik IyerSoftware Architect

Commented:
Can you close your visual studio and try running it under elevated permissions(right click on visual studio and run as administrator) and give this code a try?
Howard BashSenior Software Engineer

Author

Commented:
Good thought, but nope.
Top Expert 2015
Commented:
What is that little blue dot icon over the file icon in your first screenshot. I do not remember ever seeing something like it on a .txt file. Could it be that some application plays tricks with that file?
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Do you have file extensions hidden? If so, perhaps the filename in questions is actually "foo.txt.txt". I've done that on a few occasions. If your file extensions are hidden, try un-hiding them to see if the extension is what you think it is.
Just a thought, do you have file extension hidden in windows? I am asking because I want to make sure your file is not "C:\mycode\datafiles\foo.txt.txt"
Howard BashSenior Software Engineer

Author

Commented:
The icon is for text files (sometimes) in windows 10.  Here is the property of the file I am attempting to read:

FileReadCrash3.jpg


FileReadCrash4.jpg
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
What output do you get if you make the following call in the Immediate Window:

?System.IO.Directory.GetFiles(@"C:\mycode\datafiles")

Open in new window

Howard BashSenior Software Engineer

Author

Commented:
Indeed I was hiding the file extensions and that was all of it.  By doing a getfiles and looking at the return I saw the extra .txt.  I then unhide the known extensions and there they were!!!

Thanks folks!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial