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