Skip to content

Instantly share code, notes, and snippets.

@lindexi
Created February 23, 2018 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lindexi/b1e54915b3513936149f748c0ba67bdc to your computer and use it in GitHub Desktop.
Save lindexi/b1e54915b3513936149f748c0ba67bdc to your computer and use it in GitHub Desktop.
替换不能做文件名的字符
public static string MakeValidFileName(string text, string replacement = "_")
{
StringBuilder str=new StringBuilder();
var invalidFileNameChars = System.IO.Path.GetInvalidFileNameChars();
foreach (var c in text)
{
if (invalidFileNameChars.Contains(c))
{
str.Append(replacement??"");
}
else
{
str.Append(c);
}
}
return str.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment