去除文件名的特殊字符

/// <summary>
/// 去除特殊字符
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string MakeValidFileName(string text)
{
    StringBuilder str = new StringBuilder();
    var invalidFileNameChars = Path.GetInvalidFileNameChars();
    foreach (var c in text)
    {
        if (!invalidFileNameChars.Contains(c))
        {
            str.Append(c);
        }
    }

    return str.ToString();
}

 

posted @ 2024-01-22 14:49  _York  阅读(11)  评论(0编辑  收藏  举报