根据MSDN,以下字符不能是文件名的一部分:
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)
在.net中提供api以查找文件名中不允许的字符
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 ( ) and tab (t).
但在备注部分,据说它取决于文件系统.
将此api用于基于Windows的操作系统如XP和Windows 7是否安全?
解决方法
是的,在基于ASCII的文件系统中,Path.GetInvalidFileNameChars()将保证您有一个安全的文件名.如果您检查
ASCII chart here,您会发现排除了左列中的所有内容,并且还排除了其余列中的某些字符.检查返回数组中每个char的十进制表示形式,以获取排除内容的完整列表.
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|