Kil*_*are 12 c# localization filenotfoundexception 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);
}
Run Code Online (Sandbox Code Playgroud)
Kil*_*are 29
好的发现了问题.
确定操作系统正在将资源管理器中显示的文件读作"debug.txt"作为"debug.txt.txt".
这是通过调用System.IO.Directory.GetFiles来列出目标目录中的文件来确定的.
如果我删除.txt扩展名以便Windows资源管理器将其显示为"debug",则会找到该文件.
事实证明,资源管理器在目标计算机上隐藏了已知类型的文件扩展名.
仅供参考------------------------------------------------- ---------------
打开Explorer,选择Tools-> Folder Options,然后选择View选项卡.
向下滚动并取消选中"隐藏已知文件类型的扩展名".
要确保您位于正确的文件夹中,请查看 Environment.SpecialFolders
例如
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
Run Code Online (Sandbox Code Playgroud)
然后还要检查特定文件的权限.