System.IO.Path.GetInvalidPathChars()
给出无效字符列表。
但我需要列出所有有效字符。
想到的第一个简单的想法是从 0 迭代到 255 并排除无效字符,但它只会给出有效字符吗?还有 Unicode 呢?我应该从 0 迭代到 65535 吗?
AFAIK .NET 中没有任何东西可以获取此信息。
您可以查看以下微软页面:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
相关摘录:
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)
Integer value zero, sometimes referred to as the ASCII NUL character.
Characters whose integer representations are in the range from 1 through 31,
except for alternate data streams where these characters are allowed.
Any other character that the target file system does not allow.
这个怎么样:
private static IEnumerable<char> GetValidFileNameChars()
{
var invalidChars = new List<char>(System.IO.Path.GetInvalidFileNameChars());
var allChars = new List<char>();
for (var i = 0; i < 255; i++)
{
allChars.Add((char) i);
}
var validChars = allChars.AsEnumerable().Except(invalidChars.AsEnumerable());
return validChars;
}
或者,您可以从 0 迭代到 65535 ( char.MinValue
to char.MaxValue
)。