微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 文件名中的无效字符

根据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 (\0) and tab (\t).

但在备注部分,据说它取决于文件系统.

将此api用于基于Windows的操作系统如XP和Windows 7是否安全?

解决方法

是的,在基于ASCII的文件系统中,Path.GetInvalidFileNameChars()将保证您有一个安全的文件名.如果您检查 ASCII chart here,您会发现排除了左列中的所有内容,并且还排除了其余列中的某些字符.检查返回数组中每个char的十进制表示形式,以获取排除内容的完整列表.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐