Link to home
Start Free TrialLog in
Avatar of Howard Bash
Howard BashFlag for United States of America

asked on

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?

User generated image
Avatar of sachiek
sachiek
Flag of Singapore image

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

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

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

Avatar of Howard Bash

ASKER

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.
Since File.Exists says no file,  getting it's length shouldn't work, no?
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.
User generated imageThis is my local machine.  I checked permissions on the temp folder (which I created for this test) and the folder gives users:
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?
Good thought, but nope.
SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The icon is for text files (sometimes) in windows 10.  Here is the property of the file I am attempting to read:

User generated image


User generated image
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

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!!!