C# 去除文件非法字符名

            string resultFileName = MD5Encrypt(NavigateUrl).Replace("=",string.Empty) + ".txt";
            string newFilename = string.Empty;

            //StringBuilder rBuilder = new StringBuilder(resultFileName);
            //foreach (char rInvalidChar in Path.GetInvalidFileNameChars())
            //{
            //    rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
            //}

或者使用linq中的Aggregate
            var invalidFileName = Path.GetInvalidFileNameChars();
            newFilename = invalidFileName.Aggregate(resultFileName, (o, r) => (o.Replace(r.ToString(),string.Empty)));

  

原文地址:https://www.cnblogs.com/c-x-a/p/7792983.html