21

I am writing a simple console application that will read xml from a test file and deserialize it to an object.

var s = File.ReadAllBytes("‪G:\\Temp\\Publishing\\2.txt");
Stream _response = File.OpenRead("‪G:\\Temp\\Publishing\\2.txt");
var s = File.ReadAllBytes(@"‪g:\temp\publishing\2.txt");
var s = File.ReadAllBytes(@"‪G:\Temp\Publishing\2.txt");

I have tried all of the above to read the file and it always throws NotSupportedException with a message

The given path's format is not supported.

What is the format-error in the above path?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
Libin TK
  • 1,477
  • 2
  • 25
  • 46
  • Furthermore, unrelated, is there a reason you're using `ReadAllBytes` rather than getting a `StreamReader` for the path and calling `ReadToEnd()`? Or better yet, loading the path right into the XML parsing code you've got (for instance, `XElement.Load(string)`? – Matthew Haugen Jul 21 '14 at 02:19
  • @MatthewHaugen: The question says that they all throw the error. I would counter saying "Why would you use a StreamReader when ReadAllBytes does what you actually want?" – Billy ONeal Jul 21 '14 at 02:20
  • @BillyONeal touché. I read too quickly for that one. Sorry about that. Deleting my comment. As for your second point, I completely agree, but I thought it might be a beneficial step to debugging. It seems like a weird issue, so maybe it will have a weird solution. Not to mention, if the OP wasn't aware of either option, I'd much rather bring them up than extensively troubleshoot for something that isn't the best way to do it. – Matthew Haugen Jul 21 '14 at 02:24
  • @MatthewHaugen: "Just try different things" is usually not a good debugging strategy. – Billy ONeal Jul 21 '14 at 02:25
  • @MatthewHaugen I was just trying to see if it works. `Stream _response = File.OpenRead(@"‪G:\Temp\Publishing\2.txt");` is the original one. – Libin TK Jul 21 '14 at 02:30
  • Is your path really inline like you show? Even an invalid drive letter, extra slash, and other things don't get a `NotSupportedException`. So far the only thing I can get a `NotSupportedException` with is an extra colon. – Dave Cousineau Jul 21 '14 at 02:31
  • I was seeing this issue for over a month. For almost all drives and directories. – Libin TK Jul 21 '14 at 02:31
  • @BillyONeal I would agree for most cases, but this is an outlier. From what we see, it should definitely be working. So if that *did* by some magical occurrence fix something, that would give us one more piece of information. But again, my main point was to bring to light the other options that might be more ideal for the problem at hand. It really wasn't meant to become a big deal, just a side-note. Hence, "furthermore, unrelated,...." – Matthew Haugen Jul 21 '14 at 02:32
  • @Sahuagin yes, above lines are my actual code. – Libin TK Jul 21 '14 at 02:32
  • @LibinTK yeah, this is a weird one. Have you tried with a new file, or even new project? That *might* help prove or disprove Billy's answer suggestion. It sounds like the most likely scenario. Remember, of course, not to copy and paste any code if you do choose to try that. – Matthew Haugen Jul 21 '14 at 02:33
  • 1
    @LibinTK I would try deleting and retyping the whole path. Don't copy it from anywhere, either. If there is a zero-width character as Billy ONeal suggests, you may be copying it between each line. – Dave Cousineau Jul 21 '14 at 02:34
  • Yes, I have retyped and it works. :) – Libin TK Jul 21 '14 at 02:40
  • Is everybody is having this issue or is it just me (Windows 8.1 Pro and VS Ultimate 2013 Update 2)? – Libin TK Jul 21 '14 at 02:48

1 Answers1

39

According to the reference source: http://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs#732

NotSupportedException will be thrown if the index of the : in your path is at the third position or later. (One would expect : to be the second character) Are you sure there are no zero-width combining characters or other similar Unicode shenanigans going on in your source?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • 5
    I just checked the index using `string _path = @"‪G:\Temp\Publishing\2.txt";int i = path.IndexOf(':');` and surprisingly index of `:` is 2. But how/why? – Libin TK Jul 21 '14 at 02:36
  • 18
    I had copied the path from `Windows Explorer` and it caused something which I still understand to add an unknown/hidden character in the path string and pushed `:` to 3rd position. I have retyped the complete path and it works. – Libin TK Jul 21 '14 at 02:44
  • 8
    @LibinTK I pasted your example into Visual Studio, saved it as a text file, and opened it up in a text editor. There's a `U+202A LEFT-TO-RIGHT EMBEDDING` character in between the " and the G. – Billy ONeal Apr 13 '15 at 19:01
  • Nicely spotted @BillyONeal !! :-) – AndyS Oct 06 '15 at 11:06
  • The link seems to be (effectively) broken. – Peter Mortensen Jan 15 '16 at 16:45
  • @Peter: The specific line moved around a bit but the link does work. Perhaps ReferenceSource was not having a good time before, because I tried the link this morning and it was indeed borked. The specific line I was referring to moved a bit so I updated the link to the correct location now. Thanks! – Billy ONeal Jan 16 '16 at 10:36
  • 3
    @LibinTK thanks for that, saved me a lot of time! Copied from Windows explorer was throwing this error. Manually typed it and it was fine. – Tom Gullen Sep 06 '16 at 12:30
  • Thank god man, copied links from outsources or properties (in my example property page of the folder) have a single silent character which is invisible. – Alper Mar 06 '18 at 13:51
  • 1
    In my case the problem was there were some "invisible" characters in the path string since I copied the path directly from "Security" tab of the file's properties. When I wrote the path instead of copy/paste the problem has gone. You check this answer for more information : https://stackoverflow.com/a/53580908/11741043 – Ali Ihsan Elmas Jan 05 '21 at 13:00