可能重複:
How check if given string is legal (allowed) file name under Windows?C#刪除不允許的文件夾名稱的字符
找遍了一下,花了幾分鐘谷歌搜索,但我不能把我所發現,我的背景。 。
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string fname = projectNameBox.Text;
if (projectNameBox.TextLength != 0)
{
File.Create(appPath + "\\projects\\" + fname + ".wtsprn");
所以,我檢索projectNameBox.Text和與文本創建一個文件作爲文件名,但如果我包括一個:,或一個\或一個/等..它只會崩潰,這是正常的,因爲這些是不允許的文件夾名稱..我如何檢查文本,創建文件之前,並刪除角色,甚至更好,什麼都不做,並建議用戶他不能使用這些角色? 預先感謝
是否使用的WinForms或WPF? –
System.IO.Path.GetInvalidPathChars(): – eulerfx
我正在使用Windows窗體,抱歉沒有指定!和eulerfx..how我可以適應這種情況下..這讓我困惑! –