C#中用于文件名验证的正则表达式 [英] Regular expressions in C# for file name validation
本文介绍了C#中用于文件名验证的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是好的正则表达式,可以验证文本字符串以确保它是有效的Windows文件名?(又没有 \/:*?<> |
字符).
What is a good regular expression that can validate a text string to make sure it is a valid Windows filename? (AKA not have \/:*?"<>|
characters).
我想像以下那样使用它:
I'd like to use it like the following:
// Return true if string is invalid.
if (Regex.IsMatch(szFileName, "<your regex string>"))
{
// Tell user to reformat their filename.
}
推荐答案
正如已经回答的那样,GetInvalidFileNameChars应该为您做到这一点,并且您甚至不需要正则表达式的开销:
As answered already, GetInvalidFileNameChars should do it for you, and you don't even need the overhead of regular expressions:
if (proposedFilename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
{
MessageBox.Show("The filename is invalid");
return;
}
这篇关于C#中用于文件名验证的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文