繁体   English   中英

C#找不到文件

[英]C# Can't find file

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

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

异常错误:

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

我正在从 Python 移植我的代码,并且从来没有遇到过任何问题。 提前致谢!

这很奇怪。 电脑似乎坚信D:\\Users\\Andrey\\Desktop\\Turn\\character\\目录下没有attacks.txt文件,但是你说它肯定存在。 你们中的一个人一定是错的,多年来我了解到计算机比我更经常是正确的。

您确定您的文件实际上具有.txt扩展名,而不是.txt.somethingelse扩展名吗? 如果您在 Windows shell 中关闭了文件扩展名的显示,您可能会丢失这个额外的文件扩展名。 不过,计算机内部并没有遗漏它,并且将其视为与您请求的文件完全不同的文件。 家伙也遇到了同样的问题。

重新配置 Windows 资源管理器:

  1. 打开控制面板文件夹。
  2. 单击“文件夹选项”。
  3. 切换到“查看”选项卡。
  4. 在“高级设置”列表框中的项目列表中找到“显示隐藏的文件、文件夹和驱动器”单选按钮,并确保将其选中。
  5. 单击确定。

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

  1. 确保如果在字符串文字中使用反斜杠(这是标准的 Windows 路径分隔符),则使用第二个反斜杠“转义”它们。 之所以需要这样做,是因为 C# 编译器将反斜杠解释为转义字符,这允许您键入诸如\\t类的内容来插入制表符。 如果要插入常规反斜杠,则必须转义转义符 — \\\\ 这就是当您尝试使用单个反斜杠时出现错误的原因。

  2. 除了转义反斜杠字符,您还可以告诉 C# 编译器准确地解释您输入的字符串文字,包括空格。 这些被称为逐字字符串文字 您可以通过在字符串前加上@字符来完成此操作。 例如: @"C:\\MyDirectory\\MyFile.txt"

  3. 您现在使用的正斜杠字符之所以有效,完全是出于向后兼容性的原因。 这不是错误的原因(正如您从异常消息中看到的,其中包括正在搜索的路径),但在路径中使用正斜杠可能仍然不是一个好主意。 正如我上面提到的,反斜杠是 Windows 中的标准路径分隔符。

这是因为您创建了一个文本文件,所以它自动带有 .txt 扩展名。 因此,如果您故意添加.txt,那么该文件现在实际上是attacks.txt.txt。

我认为因为你的文件名中有一个“\\”,它可能会把它弄乱并告诉它在字符文件夹中查找attacks.txt文件。

编辑:或者至少看起来你告诉它你的文件中有斜杠。 它在错误中为您提供的文件路径是文件在您机器上的实际位置吗?

是的,如果你用 '\\' 替换 '/',你会得到一个无效的字符错误。 用 2 个斜线试试这个

System.IO.StreamReader file = new System.IO.StreamReader("character\\attacks.txt");

要么

System.IO.StreamReader file = new System.IO.StreamReader(@"character\attacks.txt");
  1. Posix路径拆分器是/而在Windows\\
  2. 使用Directory.GetCurrentDirectory确保当前目录正确。
  3. 如果当前目录不正确,请使用完整路径或通过Directory.SetCurrentDirectory更改当前目录

这将工作正常...

 string fileName = file_name+".pdf";
                string files = System.IO.Path.Combine("pdf/") + fileName;
                if (!System.IO.File.Exists(files))
                {
                    return RedirectToAction("index", new { message = "File Not Found" });

                }
                else
                {

                    byte[] fileBytes = System.IO.File.ReadAllBytes(files);
                    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, files);

                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM