Created
May 22, 2022 08:08
-
-
Save demndevel/be77e73b0138eba365f8e8d8030cc7ac to your computer and use it in GitHub Desktop.
Sanitize filename c sharp
This file contains 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 class Sanitizer | |
{ | |
public static string Sanitize(string name) | |
{ | |
string invalidChars = System.Text.RegularExpressions.Regex.Escape( new string( System.IO.Path.GetInvalidFileNameChars() ) ); | |
string invalidRegStr = string.Format( @"([{0}]*\.+$)|([{0}]+)", invalidChars ); | |
return System.Text.RegularExpressions.Regex.Replace( name, invalidRegStr, "_" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment