PuntanetDeveloper
PuntanetDeveloper

Reputation: 318

file open dotnet core 2.1 System.IO.IOException

I'm creating a dotnet core 2.1 project when I try to open a file I get this error System.IO.IOException because it looks for the file under netcoreapp2.1

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

and error is

   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

Upvotes: 0

Views: 371

Answers (1)

Turbo
Turbo

Reputation: 2561

Because slashes are not escaped, FileStream is assuming the path to be relative to the current folder. Use literal string or escape the path.

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

Upvotes: 1

Related Questions