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)));

  

posted @ 2017-11-06 14:23  公众号python学习开发  阅读(334)  评论(0编辑  收藏  举报