我如何检查,如果给定的字符串是Windows下的一个法律/有效的文件名? [英] How do I check if a given string is a legal / valid file name under Windows?

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

问题描述

我想包括我的应用程序的批处理文件重命名功能。用户可以我需要检查它是否将是Windows下的法律文件名(在模式替换一些通配符之后)键入一个目标文件名图案。我试图用正前pression像 [A-ZA-Z0-9 _] + ,但它不包括各种语言中的许多具体的国家字符(例如变音符号等)。什么是做这种检查的最佳方式?

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?

推荐答案

您可以从<一个无效字符的列表href=\"http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars.aspx\"><$c$c>Path.GetInvalidPathChars和<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx\"><$c$c>GetInvalidFileNameChars.

UPD:查看<一个href=\"http://stackoverflow.com/questions/62771/how-check-if-given-string-is-legal-allowed-file-name-under-windows#62855\">Steve库珀的建议的关于如何在常规的前pression使用这些。

UPD: See Steve Cooper's suggestion on how to use these in a regular expression.

UPD2:请注意,根据MSDN上的备注部分数组此方法返回不保证包含一整套在文件和目录名无效字符的。 <一href=\"http://stackoverflow.com/questions/62771/how-check-if-given-string-is-legal-allowed-file-name-under-windows/62888#62888\">The回答sixlettervaliables提供进入更多的细节。

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天全站免登陆