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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With