StreamReader 抱怨文件不存在,但它确实存在
我有一个已本地化以供在欧洲使用的应用程序。
我有一个从磁盘加载文件的菜单选项。
此操作在我的开发计算机上运行良好,但在我用来测试其他操作系统(例如法语、西班牙语等)的虚拟机上不起作用。
当 StreamReader 尝试打开文件时,会生成 FileNotFoundException。
它说“'找不到文件 C:\Program Files\MyCompany\MyTool\bin\Files\debug.txt'”
事实是,该文件确实存在,位于正确的位置并具有正确的文件名。
目标(法语)操作系统上的目录名称与开发计算机相同。
有什么想法吗?
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的找到问题了。
确定操作系统正在将资源管理器中显示为“debug.txt”的文件读取为“debug.txt.txt”。
这是通过调用 System.IO.Directory.GetFiles 列出目标目录中的文件来确定的。
如果我删除 .txt 扩展名,以便 Windows 资源管理器将其显示为“调试”,则可以找到该文件。
事实证明,资源管理器隐藏了目标计算机上已知类型的文件扩展名。
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.
要确保您位于正确的文件夹中,请查看
Environment.SpecialFolders
例如,
然后还要检查特定文件的权限。
To make sure you're in the correct folder, look at
Environment.SpecialFolders
e.g.
Then also check the permissions on the specific file.
我也会
在打开之前尝试使用。 使用。
一个小建议是在组合路径的两个部分时
I would also try to use
before opening it. And a little advice is to use
When combining 2 parts of a path.
也许该前缀是错误的:
C:\Program Files
例如,对于巴西葡萄牙语 Windows 安装,该文件夹变为
"C:\Arquivos de Programas\"
;您应该确保您的 Windows 安装没有相同的“功能”。如果该示例代码在该文件夹内运行,您可以使用相对路径。
您也可以尝试使用
ourPath = "%ProgramFiles%\MyCompany\MyTool\
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....