Asp.net中文件夹名称的正则表达式 [英] Regular Expression in Asp.net for Folder Name
本文介绍了Asp.net中文件夹名称的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证用于创建文件夹的字符串.
所以我想限制用户输入\ / : ? " < > |
之类的字符
此字符将使文件夹创建失败.
任何人都可以通过给我正则表达式来验证此字符的方式来帮助我吗?
谢谢
[edit]删除了空格[/edit]
I want to validate a string that is used to create a folder.
so I want to restrict user from entering characters like \ / : ? " < > |
this characters will down the folder creation.
Can anyone help me by giving me the regular expression to validate this characters?
Thank You
[edit]removed white spaces[/edit]
推荐答案
string abc = "anvas the realnapster /";
char[] SpecialChars = "\/:?<>|".ToCharArray();
int indexOf = abc.IndexOfAny(SpecialChars);
if (indexOf != -1)
{
//there should a special char in the string
}
else
{
//string is fine
}
Anvas的想法正确,但是.NET提供了两种方法,它们返回文件和路径的非法字符,您应该使用这些方法来代替定义自己的字符:
GetInvalidPathChars [ GetInvalidFileNameChars [
Anvas has the right idea, but .NET provides two methods which return the illegal characters for a file and a path, which you should use instead of defining your own:
GetInvalidPathChars [^]
GetInvalidFileNameChars[^]
if (myFileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
{
// Illegal character in file name
}
与指定自己的端口相比,使用这些端口可提供更好的可移植性.
Using these provides better portability than specifying your own.
这篇关于Asp.net中文件夹名称的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文