Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamReader complains that file does not exist, but it does

I have an application that is localized for use across Europe.

I have a menu option that loads a file from disk.

This operation works fine on my dev machine but does not work on the virtual machine I use to test other operating systems _ e.g French, Spanish etc.

A FileNotFoundException is generated when the StreamReader tries to open the file.

It says "'Could not find the file C:\Program Files\MyCompany\MyTool\bin\Files\debug.txt'"

Thing is, the file does exist, at the correct location and with the correct filename.

The directory names on the target (French) operating system are the same as the dev machine.

Any ideas?

string ourPath =   System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

           try
        {
            System.IO.StreamReader sr = System.IO.File.OpenText(ourPath + @"\bin\Files\debug.txt");
            string input = null;
            while ((input = sr.ReadLine()) != null)
            {
                m_text.Append(input);
            }
            sr.Close();
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("LoadDebugOptions: File Not Found: " + ex.Message);
        }
like image 660
Kildareflare Avatar asked Jan 27 '10 12:01

Kildareflare


People also ask

What happens if StreamReader is not closed?

@IvanLi It automatically disposes of the StreamWriting. Anything declared in a using statement is disposed of when the statement ends.

What does StreamReader mean in C#?

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.

How does a StreamReader differ from a StreamWriter?

A StreamReader is used whenever data is required to be read from a file. A Streamwriter is used whenever data needs to be written to a file.


2 Answers

Ok found the problem.

Determined that the operating system was reading the file displayed in explorer as "debug.txt" as "debug.txt.txt".

This was determined by using a call to System.IO.Directory.GetFiles to list the files in the target directory.

If I remove the .txt extension so that windows explorer displays it as "debug" then the file is found.

Turns out explorer was hiding file extensions of known types on the target machine.

FYI ----------------------------------------------------------------

Open Explorer, Select Tools->Folder Options then the View Tab.

Scroll down and uncheck "Hide extensions for Known file types".

like image 125
Kildareflare Avatar answered Oct 24 '22 21:10

Kildareflare


To make sure you're in the correct folder, look at Environment.SpecialFolders

e.g.

string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

Then also check the permissions on the specific file.

like image 37
ZombieSheep Avatar answered Oct 24 '22 19:10

ZombieSheep