StreamReader complains that file does not exist, but it does
Asked Answered
D

5

13

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);
        }
Darter answered 27/1, 2010 at 12:4 Comment(1)
Try using File.Exists method to verify if the file exist.Lyndonlyndsay
D
30

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".

Darter answered 27/1, 2010 at 14:11 Comment(1)
Thank you so much. I had a similar problem with my config.json file. Following your method, found it was read as config.json.txt.Rusticate
C
5

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.

Comeon answered 27/1, 2010 at 12:16 Comment(1)
Unfortunately I cannot use an explicit folder this way, since the end user may install the application somewhere else. This is why I get the directory path of where the application is installed (see Edit). Also, I've checked that the folder is called Program Files on the target machine.Darter
E
4

I would also try to use

File.Exists()

before opening it. And a little advice is to use

Path.Combine()

When combining 2 parts of a path.

Equiponderate answered 27/1, 2010 at 12:36 Comment(2)
Be careful with Path.Combine if there's any chance of user input making it there: https://mcmap.net/q/117642/-why-does-path-combine-not-properly-concatenate-filenames-that-start-with-path-directoryseparatorchar/522859Rhodie
Thanks Chris, indeed, a check may be required !Equiponderate
C
1

Maybe that prefix is wrong: C:\Program Files

For example, for Brazilian Portuguese Windows installations that folder becomes "C:\Arquivos de Programas\"; you should to make sure your windows installations doesn't have same "feature".

If that sample code runs inside that folder, you could to use a relative path.

You also could try to use ourPath = "%ProgramFiles%\MyCompany\MyTool\

Companionway answered 27/1, 2010 at 12:13 Comment(0)
F
1

It may be due to security exception as the current user trying to read does not have sufficient permission. I have encountered that many times....

Farmland answered 27/1, 2010 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.