C# StreamReader 抱怨文件不存在,但它确实存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2146652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StreamReader complains that file does not exist, but it does
提问by Kildareflare
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.
当 StreamReader 尝试打开文件时,会生成 FileNotFoundException。
It says "'Could not find the file C:\Program Files\MyCompany\MyTool\bin\Files\debug.txt'"
它说“'找不到文件 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);
}
采纳答案by Kildareflare
Ok found the problem.
好的发现问题了。
Determined that the operating system was reading the file displayed in explorer as "debug.txt" as "debug.txt.txt".
确定操作系统正在将资源管理器中显示为“debug.txt”的文件读取为“debug.txt.txt”。
This was determined by using a call to System.IO.Directory.GetFiles to list the files in the target directory.
这是通过调用 System.IO.Directory.GetFiles 来列出目标目录中的文件来确定的。
If I remove the .txt extension so that windows explorer displays it as "debug" then the file is found.
如果我删除 .txt 扩展名,以便 Windows 资源管理器将其显示为“调试”,则找到该文件。
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".
供参考 - - - - - - - - - - - - - - - - - - - - - - - - - ---------------
打开资源管理器,选择工具->文件夹选项,然后选择查看选项卡。
向下滚动并取消选中“隐藏已知文件类型的扩展名”。
回答by Rubens Farias
Maybe that prefix is wrong: C:\Program Files
也许这个前缀是错误的: 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".
例如,对于巴西葡萄牙语 Windows 安装,该文件夹变为"C:\Arquivos de Programas\"
; 您应该确保您的 Windows 安装没有相同的“功能”。
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\
你也可以尝试使用 ourPath = "%ProgramFiles%\MyCompany\MyTool\
回答by chugh97
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....
这可能是由于安全异常,因为当前尝试读取的用户没有足够的权限。我遇到过很多次了......
回答by ZombieSheep
To make sure you're in the correct folder, look at Environment.SpecialFolders
要确保您位于正确的文件夹中,请查看 Environment.SpecialFolders
e.g.
例如
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
Then also check the permissions on the specific file.
然后还要检查特定文件的权限。
回答by Shimrod
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.
组合路径的 2 个部分时。