C# File Reading Fails

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
LVL 1
Howard BashSenior Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sachiekCommented:
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

0
sachiekCommented:
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

0
sachiekCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Howard BashSenior Software EngineerAuthor 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.
0
Howard BashSenior Software EngineerAuthor Commented:
Since File.Exists says no file,  getting it's length shouldn't work, no?
0
sachiekCommented:
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.
0
Howard BashSenior Software EngineerAuthor 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:
0
Karrtik IyerSoftware ArchitectCommented:
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?
0
Howard BashSenior Software EngineerAuthor Commented:
Good thought, but nope.
0
Jacques Bourgeois (James Burger)PresidentCommented:
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?
0
käµfm³d 👽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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Najam UddinCommented:
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"
0
Howard BashSenior Software EngineerAuthor 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
0
käµfm³d 👽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

1
Howard BashSenior Software EngineerAuthor 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!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.