Invalid characters in a filename on Windows?

穿精又带淫゛_ 提交于 2019-12-14 03:43:44

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/11721147/invalid-characters-in-a-filename-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!