C#创建目录,文件名过滤特殊字符串,非法字符

 

 

string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
    illegal = illegal.Replace(c.ToString(), ""); 
}

 

public string RemoveInvalidFileNameChars(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName)) return null;

        string invalid = new string(Path.GetInvalidFileNameChars()); //new string(Path.GetInvalidPathChars());
        StringBuilder sb = new StringBuilder();
        //foreach (char c in invalid) {fileName = fileName.Replace(c.ToString(), "");}
        foreach (char c in fileName)
        {
            if (!invalid.Contains(c)) sb.Append(c);
        }
        return sb.ToString();
    }

 

上一篇:Vue 项目中出现 Invalid Host header 解决办法


下一篇:踩坑日记i