問題描述
我有一個本地化的應用程序,可以在整個歐洲使用.
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.
當 StreamReader 嘗試打開文件時會生成 FileNotFoundException.
A FileNotFoundException is generated when the StreamReader tries to open the file.
它說'找不到文件 C:Program FilesMyCompanyMyToolinFilesdebug.txt'"
It says "'Could not find the file C:Program FilesMyCompanyMyToolinFilesdebug.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.
有什么想法嗎?
string ourPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
try
{
System.IO.StreamReader sr = System.IO.File.OpenText(ourPath + @"inFilesdebug.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);
}
推薦答案
好的,問題找到了.
確定操作系統正在將資源管理器中顯示為debug.txt"的文件讀取為debug.txt.txt".
Determined that the operating system was reading the file displayed in explorer as "debug.txt" as "debug.txt.txt".
這是通過調用 System.IO.Directory.GetFiles 來列出目標目錄中的文件來確定的.
This was determined by using a call to System.IO.Directory.GetFiles to list the files in the target directory.
如果我刪除 .txt 擴展名,以便 Windows 資源管理器將其顯示為調試",則找到該文件.
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".
這篇關于StreamReader 抱怨文件不存在,但確實存在的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!