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

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

问题描述

我想在我的应用程序中包含批处理文件重命名功能.用户可以键入目标文件名模式,并且(在替换模式中的一些通配符之后)我需要检查它是否将成为 Windows 下的合法文件名.我尝试使用像 [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?

推荐答案

您可以从 Path.GetInvalidPathCharsGetInvalidFileNameChars.

UPD: 参见 Steve Cooper 关于如何在正则表达式中使用这些的建议.

UPD2: 请注意,根据 MSDN 中的备注部分不保证从此方法返回的数组包含在文件和目录名称中无效的完整字符集."提供的答案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天全站免登陆