如何在Windows下检查给定的字符串是否为合法/有效的文件名? [英] How do I check if a given string is a legal / valid file name under Windows?
问题描述
[a-zA-Z0-9 _] +
这样的正则表达式,但不包括来自各种语言的许多国家特定字符(例如,变音符号和所以) 您可以从 Path.GetInvalidPathChars
和 GetInvalidFileNameChars
。
UPD:请参阅史蒂夫·库珀的建议如何在正则表达式中使用这些。 >
UPD2:请注意,根据MSDN中的注释部分从此方法返回的数组不能保证包含无效的完整字符集在文件和目录名称。 提供的答案六个简报详细介绍。
I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I need to check if it's going to be a legal filename under Windows. I've tried to use regular expression like [a-zA-Z0-9_]+
but it doesn't include many national-specific characters from various languages (e.g. umlauts and so on). What is the best way to do such a check?
You can get a list of invalid characters from Path.GetInvalidPathChars
and GetInvalidFileNameChars
.
UPD: See Steve Cooper's suggestion on how to use these in a regular expression.
UPD2: Note that according to the Remarks section in MSDN "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 answer provided by sixlettervaliables goes into more details.
这篇关于如何在Windows下检查给定的字符串是否为合法/有效的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!