当我尝试打开文件时,我正在创建一个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
答案 0 :(得分:1)
因为不转义斜线,所以FileStream假定路径是相对于当前文件夹的。 使用文字字符串或转义路径。
string path = @"C:\user\name\desktop\file.txt"; // Note the @ that denotes a literal string.
FileStream file = new FileStream(path, FileMode.Open);