Reputation: 3135
As per the MSDN , the following characters cannot be part of the file name:
Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
◦The following reserved characters:
<
(less than)>
(greater than):
(colon)"
(double quote)/
(forward slash)\
(backslash)|
(vertical bar or pipe)?
(question mark)*
(asterisk)
In .net an api is provided to find the what are the characters not allowed in a filename
char[] invalidFileChars = Path.GetInvalidFileNameChars();
Remarks
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).
But in the remark section it is said that it is depend on the file system.
Is it safe to use this api for the windows based os like XP and windows 7?
Upvotes: 5
Views: 27021
Reputation: 6020
Yes, in an ASCII-based file system Path.GetInvalidFileNameChars()
will guarantee you a safe file name. If you check the ASCII chart here you will find that everything from the left column is excluded and certain characters from the remaining columns are also excluded. Check the decimal representation of each char in the returned array for a full list of what's excluded.
Upvotes: 3
Reputation: 39441
The first part is specifying what characters are prohibited by the language itself. However, since classes are loaded from files with specific names (on most platforms), the underlying platform may impose additional restrictions due to the filesystem. The same is true in Java, though you can technically get around it by using a custom classloader.
Upvotes: 0