Created
February 23, 2018 01:10
-
-
Save lindexi/b1e54915b3513936149f748c0ba67bdc to your computer and use it in GitHub Desktop.
替换不能做文件名的字符
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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