代码如下:
/// <summary>
/// 文件名-判断是否含有非法字符
/// </summary>
public void CheckFileName(){
//^[\\/:*?\"<>|]*$中英文不带\/:*?"<>|等,下面我又添加了几种
Regex regex = new Regex("^[\\/:*?\"<>|.~&+#%{}]*$");
var resultName=regex.Match(contentEdit.Text);
if (!resultName.Success)
{
DXMessageBox.Show("Windows文件名不可包含“\\/:*?\"<>|.~&+#%{}”", "提示!");
return;
}
}
/// <summary>
/// 文件名-去除非法字符
/// </summary>
/// <param name="oldFileName">原文件名</param>
/// <param name="replaceStr">用于替换非法字符的字符</param>
/// <returns></returns>
private string GetNewFileName(string oldFileName, char replaceChar)
{
// \\/:*?\"<>|.~&+#%{}
string regexStr = "\\/:*?\"<>|.~&+#%{}";
char[] regex = regexStr.ToCharArray();
char[] chars = oldFileName.ToCharArray();
for (int i = 0; i < chars.Count(); i++)
{
var result = regex.Contains(chars[i]);
if (result)
{
chars[i] = replaceChar;
}
}
return new string(chars);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
作者:꧁执笔小白꧂