StreamReader complains that file does not exist, but it does

北城以北 提交于 2019-11-30 08:47:59

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

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.

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.

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\

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!