1. 程式人生 > 其它 >C#過濾不合法的檔名或者檔案路徑字元

C#過濾不合法的檔名或者檔案路徑字元

技術標籤: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 static
string FilterInvalidPathChars(string path) { StringBuilder titleBuilder = new StringBuilder(path); foreach (char rInvalidChar in Path.GetInvalidPathChars()) { titleBuilder = titleBuilder.Replace(rInvalidChar.ToString(), string.Empty); } return titleBuilder.ToString(); }