C#找不到文件 [英] C# Can't find file

查看:301
本文介绍了C#找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#的Visual Studio 2008窗体需要读取相关文件的内容'character / attacks.txt'当我运行它时,File.Exists()返回false,即使我是积极的我的目录是排序。代码:

  try 
{
System.IO.StreamReader file = new System.IO.StreamReader 字符/ attacks.txt);
int counter = 0;
int numberOfLines = 0;
字符串行; ((line = file.ReadLine())!= null){numberOfLines ++;
while }
string []攻击=新字符串[numberOfLines]; ((line = file.ReadLine())!= null){攻击[counter] =行;
while计数器++; }
file.Close();
print(attacks.ToString());

catch(Exception ex)
{
print(ERROR);打印(您是否编辑过任何文件?);打印(ex.ToString());



$ b异常错误:





$ b

  System.IO.FileNotFoundException:找不到文件'D:\ Users \Andrey\Desktop\Turn\character\attacks.txt'。 
文件名:'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'在System.IO中的
.__ Error.WinIOError(Int32 errorCode,String maybeFullPath)
at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy)
在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项)
在System.IO.StreamReader..ctor(字符串路径,编码编码,布尔detectEncodingFromByteOrderMarks ,Int32 bufferSize)
at System.IO.StreamReader..ctor(String path)
at TurnByTurn.Form1.button1_Click(Object sender,EventArgs e)in D:\ Users \Andrey\Desktop \C#\TurnByTurn\TurnByTurn\Form1.cs:第52行

我正在移植我从Python的代码,永远不会对此有任何的麻烦。提前致谢!

解决方案

这很奇怪。电脑似乎坚信在 D:\ Users \Andrey\Desktop\Turn\中没有 attacks.txt 文件字符\ 目录,但你说它确实存在。你们其中一个一定是错的,多年来我学会了电脑比我更经常。

你确定你的文件实际上有一个 .txt 扩展名,而不是。 txt.somethingelse 扩展名?如果在Windows外壳中关闭了文件扩展名的显示,则可能会丢失这个额外的文件扩展名。计算机不会在内部丢失,而是将它视为与请求的文件完全不同的文件。这就是这个人所遇到的问题。



配置Windows资源管理器:
$ b $ ol

  • 打开控制面板文件夹。
  • 单击文件夹选项。

  • 切换到查看标签。
  • 找到项目列表中的显示隐藏的文件,文件夹和驱动器单选按钮高级设置列表框,并确保它被选中。

  • 单击确定。



  • 这个问题的其他答案也提出了一些很好的建议。其中:


    1. 确保如果在字符串文字中使用反斜线(这是标准的Windows路径分隔符)你用第二个反斜杠逃避它们。这是必需的原因是C#编译器将反斜杠解释为转义字符,它允许您键入像 \ t 之类的内容来插入选项卡。如果你想插入一个常规的反斜杠,你必须逃避 - \\ 。这就是当您试图使用单个反斜杠时出现错误的原因。

    2. 你的字符串文字正好是,因为它是键入的,包括空格。这些被称为逐字字符串文字。您可以通过在 @ 字符前添加字符串来完成此操作。例如: @C:\MyDirectory\MyFile.txt


    3. 您现在使用的正斜杠字符严格来说是出于向后兼容的原因。这不是错误的原因(正如您可以从异常消息中看到的,其中包括正在搜索的路径),但是在路径中使用正斜杠可能不是一个好主意。如上所述,反斜杠是Windows中的标准路径分隔符。



    I have a C# Visual Studio 2008 Form which needs to read the contents of the relative file 'character/attacks.txt' File.Exists() returns false when I run it, even though I'm positive my directories are sorted. Code:

                try
                {
                    System.IO.StreamReader file = new System.IO.StreamReader("character/attacks.txt");
                    int counter = 0;
                    int numberOfLines = 0;
                    string line;
                    while ((line = file.ReadLine()) != null) { numberOfLines++; }
                    string[] attacks = new string[numberOfLines];
                    while ((line = file.ReadLine()) != null) { attacks[counter] = line; counter++; }
                    file.Close();
                    print(attacks.ToString());
                }
                catch (Exception ex) 
                {
                    print("ERROR"); print("Did you edit any files?"); print(ex.ToString()); 
                }
    

    Exception Error:

    System.IO.FileNotFoundException: Could not find file 'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'.
    File name: 'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
       at System.IO.StreamReader..ctor(String path)
       at TurnByTurn.Form1.button1_Click(Object sender, EventArgs e) in D:\Users\Andrey\Desktop\C#\TurnByTurn\TurnByTurn\Form1.cs:line 52
    

    I'm porting my code from Python and never had any troubles with this. Thanks in advance!

    解决方案

    That's very strange. The computer seems firmly convinced that there is no attacks.txt file in the D:\Users\Andrey\Desktop\Turn\character\ directory, but you say that it definitely exists. One of you must be wrong, and I've learned over the years that computers are right more often than I am.

    Are you sure that your file actually has a .txt extension, not a .txt.somethingelse extension? If you have the display of file extensions turned off in the Windows shell, you might be missing this extra file extension. The computer isn't missing it internally, though, and it's seeing that as a totally different file than the one you requested. That's the same problem this guy had.

    To re-configure Windows Explorer:

    1. Open the Control Panel folder.
    2. Click on "Folder Options".
    3. Switch to the "View" tab.
    4. Find the "Show hidden files, folders, and drives" radio button in the list of items in the "Advanced settings" list box, and make sure that it is selected.
    5. Click OK.

    The other answers to this question do make some good suggestions. Among them:

    1. Make sure that if you use backslashes (which is the standard Windows path separator character) in your string literals, you "escape" them using a second backslash. The reason this is required is that the backslash is interpreted as an escape character by the C# compiler, which allows you to type things like \t to insert a tab. If you want to insert a regular backslash, you have to escape the escape—\\. That's the reason you were getting an error when you tried to use a single backslash.

    2. Instead of escaping the backslash character, you can tell the C# compiler to interpret your string literal exactly as it is typed, including spaces. These are called verbatim string literals. You do this by prefixing the string with the @ character. For example: @"C:\MyDirectory\MyFile.txt"

    3. The reason why the forward slash character that you're using now works is strictly for backwards-compatibility reasons. It isn't the cause of the error (as you can see from the exception message, which includes the path being searched), but it's probably still not a good idea to use forward slashes in paths. As I mentioned above, the backslash is the standard path delimiter in Windows.

    这篇关于C#找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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