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

查看:160
本文介绍了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 Files \ MyCompany \ MyTool \ bin \ Files \ debug.txt'"

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.

有什么想法吗?

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);
        }

推荐答案

确定.

确定操作系统正在读取显示在资源管理器中的文件"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天全站免登陆