C#检查文件名的合法正则表达式 |
您所在的位置:网站首页 › 文件名合法标准 › C#检查文件名的合法正则表达式 |
引用 了网友的内容(http://tyllxx.iteye.com/blog/1411669),不过有点小问题,经本人测试,作者原来的代码,无法检查,半角的双引号、大于号以及小于号,本人小改了一下,希望本文能帮到需要用到的朋友 /// /// 文件名检查 /// /// public static Boolean CheckFileName(string fileName) { StringBuilder description = new StringBuilder(); Boolean opResult = Regex.IsMatch(fileName, @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\/\\:\\\\*\\?\\\\\\""\\\\|]{1,255}$)");
if (!opResult) { description.Append("文件名包含特殊符或系统关键字!"); }
if (description.Length > 0) { MessageBox.Show(description.ToString()); } return opResult; } 注:经验证,文件名包含全角的小括号时,无法通过验证 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |