【问题标题】:file open dotnet core 2.1 System.IO.IOException文件打开 dotnet core 2.1 System.IO.IOException
【发布时间】:2019-05-29 17:24:18
【问题描述】:

我正在创建一个 dotnet core 2.1 项目,当我尝试打开一个文件时出现此错误 System.IO.IOException,因为它在 netcoreapp2.1 下查找文件

class Program
{
   static void Main(string[] args)
   {
       string path = "C:\user\name\desktop\file.txt";
       FileStream file = new FileStream(path, FileMode.Open);
   }
}

错误是

   System.IO.IOException: The syntax of the file name, directory or volume is incorrect C:\Users\name\source\repos\app\app\bin\Debug\netcoreapp2.1\‪C:\user\name\Desktop\file.txt

【问题讨论】:

  • @John 这足以让你理解我的问题如果你想要更多,告诉我什么
  • @John 在一个简单的控制台应用程序 dotnet core 中尝试这 2 行在您的 VS 中
  • 这给了我一个编译器错误。在 2.1 中使用绝对路径对我来说很好。这就是为什么我要求minimal reproducible example

标签: c# .net-core dot


【解决方案1】:

因为斜杠没有被转义,FileStream 假设路径是相对于当前文件夹的。 使用文字字符串或转义路径。

string path = @"C:\user\name\desktop\file.txt"; // Note the @ that denotes a literal string.
FileStream file = new FileStream(path, FileMode.Open);

【讨论】:

  • 这不是问题。就目前而言,问题中的代码没有构建,因此它不能表现出 OP 提到的行为。
猜你喜欢
  • 2020-03-22
  • 2019-08-03
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
相关资源
最近更新 更多