gpt4 book ai didi

c# - Windows 文件名中的无效字符?

转载 作者:行者123 更新时间:2023-11-30 12:49:23 27 4
gpt4 key购买 nike

根据 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 的十进制表示形式,以获取已排除内容的完整列表。

关于c# - Windows 文件名中的无效字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11721147/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com