C#中文件名或文件路徑非法字符判斷方法
更新時間:2015年06月26日 09:15:58 投稿:junjie
這篇文章主要介紹了C#中文件名或文件路徑非法字符判斷方法,本文主要使用了內置的GetInvalidFileNameChars方法實現非法字符判斷,需要的朋友可以參考下
文件路徑或者保存模板出現非法字符判斷
1)不為空判斷
string strTemplateName = txtTemplateName.Text;
if (string.IsNullOrWhiteSpace(strTemplateName))
{
Show("請輸入模板名稱!", "提示", .Information, OK);
txtTemplateName.Focus();
return;
}
2)然后對strTemplateName 進行非法字符判斷
if (strTemplateName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
//含有非法字符 \ / : * ? " < > | 等
Show("模板名稱含有非法字符,請重新輸入", "錯誤", Error, OK);
txtTemplateName.Focus();
return;
}
3)path 引用系統(tǒng)的io 動態(tài)庫即可。

