如何在Windows下检查给定的字符串是否为合法/有效的文件名? [英] How do I check if a given string is a legal / valid file name under Windows?

查看:180
本文介绍了如何在Windows下检查给定的字符串是否为合法/有效的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中添加批处理文件重命名功能。用户可以键入目标文件名模式(在模式中替换一些通配符后),我需要检查它是否是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆