3

According to http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars%28v=vs.110%29.aspx Path.GetInvalidFileNameChars() is supposed to give the following output

// Note: Some characters may not be displayable on the console. 
// The output will look something like: 
// 
// The following characters are invalid in a path: 
// Char    Hex Value 
// ",      0022 
// <,      003C 
// >,      003E 
// |,      007C 
// ... 
// 
// The following characters are invalid in a filename: 
// Char    Hex Value 
// ",      0022 
// <,      003C 
// >,      003E 
// |,      007C 
// ...

However I'm only getting

Char    Hex Value
,   0000
/,  002F

http://ideone.com/UdRbCC

What's going on?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
  • 4
    It's dependent on the environment. That's the whole reason to defer this task to a library function rather than hard coding it based on your machine. Apparently the machine running that code has fairly liberal file name/path restrictions. – Servy Dec 02 '14 at 15:49
  • 1
    "The output will look *something* like..." means what it says. The same page also notes "The full set of invalid characters can vary by file system." – Paul Roub Dec 02 '14 at 15:49
  • I had a feeling that it might depend on the environment. However then if I strip the invalid chars (from `Path.GetInvalidFileNameChars()`) and do `Path.GetExtension(Filename)` it still throws an invalid characters exception. Makes me a sad panda! – Snæbjørn Dec 02 '14 at 16:02

1 Answers1

5

From the article you linked:

The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names. The full set of invalid characters can vary by file system. For example, on Windows-based desktop platforms, invalid path characters might include ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).

pquest
  • 3,151
  • 3
  • 27
  • 40
  • 2
    The "vary by file system" part is important. Just because a character is valid as far as the platform itself is concerned, doesn't mean it's valid in every mounted file system. For extra fun it can also be the other way round, a file system can allow more characters than the platform so you might not be able to work with a file that contains invalid characters. – CodesInChaos Dec 02 '14 at 16:43