Avatar of Howard Bash
Howard Bash
Flag 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?

Screen Capture of the Environment During Exception
C#.NET ProgrammingProgramming

Avatar of undefined
Last Comment
Howard Bash

8/22/2022 - Mon
sachiek

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

sachiek

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

sachiek

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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.
Howard Bash

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

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Howard Bash

ASKER
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 Iyer

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 Bash

ASKER
Good thought, but nope.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Jacques Bourgeois (James Burger)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Howard Bash

ASKER
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
kaufmed

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 Bash

ASKER
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!!!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.