Steve
Steve

Reputation: 4563

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

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?

Upvotes: 7

Views: 17478

Answers (1)

jps
jps

Reputation: 22575

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.

Upvotes: 13

Related Questions