C#過濾不合法的檔名或者檔案路徑字元
阿新 • • 發佈:2020-12-15
技術標籤:C#
/// <summary> /// 過濾不合法的檔名字元 /// </summary> /// <param name="name"></param> /// <returns></returns> public static string FilterInvalidFileNameChars(string name) { StringBuilder titleBuilder = new StringBuilder(name); foreach (char rInvalidChar in Path.
GetInvalidFileNameChars()) { titleBuilder = titleBuilder.Replace(rInvalidChar.ToString(), string.Empty); } return titleBuilder.ToString(); } /// <summary> /// 過濾不合法的檔案路徑字元 /// </summary> /// <param name="path"></param> /// <returns></returns> public staticstring FilterInvalidPathChars(string path) { StringBuilder titleBuilder = new StringBuilder(path); foreach (char rInvalidChar in Path.GetInvalidPathChars()) { titleBuilder = titleBuilder.Replace(rInvalidChar.ToString(), string.Empty); } return titleBuilder.ToString(); }