C# 文件打开dotnet core 2.1 System.IO.IOException

C# 文件打开dotnet core 2.1 System.IO.IOException,c#,.net-core,dot,C#,.net Core,Dot,我正在创建一个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, File

我正在创建一个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

由于斜杠不会转义,FileStream假定路径是相对于当前文件夹的。 使用文本字符串或转义路径

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

请你提供一个例子。@John这足以让你理解我的问题,如果你想要更多,告诉我what@John在一个简单的控制台应用程序dotnet Core中尝试VS中的这2行。这会给我一个编译器错误。在2.1中使用绝对路径对我来说很好。这就是为什么我要求一份工作,这不是问题所在。因为问题中的代码没有构建,所以它不能展示OP提到的行为。