Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filestream - The filename, directory name, or volume label syntax is incorrect

Tags:

c#

unicode

I have this C# code which is supposed to open a file.

string filePath = @"‪C:\Data\123.jpg";
FileStream fs = System.IO.File.OpenRead(filePath);

But, it breaks at second line with error message The filename, directory name, or volume label syntax is incorrect

The exception details also shows C:\\dotnet\\solution\\projectname\\‪C:\\Data\\123.jpg' . Why it goes to the project path?

like image 547
Steve Avatar asked Sep 21 '20 09:09

Steve


1 Answers

Now that's a tricky one, yet so simple.

The code above is correct, it's more or less like the example in the Microsoft documentation.

But there is an invisible Unicode character E280AA

U+202A ‪ e2 80 aa LEFT-TO-RIGHT EMBEDDING

just before the letter "C".

Therefore this doesn't work:

string filePath = @"‪C:\Data\123.jpg";

But this one does:

string filePath = @"C:\Data\123.jpg";

The first one (just the actual string) as hex code looks like this:

22E280AA433A5C446174615C3132332E6A706722

the second one doesn't have the bold sequence. You can see this in the debugger or with the help of tools like Notepad++ where you can use Extensions/Converter/ASCII->HEX to see the hex code.

like image 172
jps Avatar answered Nov 10 '22 20:11

jps