StreamReader 抱怨文件不存在,但确实存在 [英] StreamReader complains that file does not exist, but it does

查看:24
本文介绍了StreamReader 抱怨文件不存在,但确实存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地化的应用程序,可以在整个欧洲使用.

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 抱怨文件不存在,但确实存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆