Skip to content

Instantly share code, notes, and snippets.

@demndevel
Created May 22, 2022 08:08
Show Gist options
  • Save demndevel/be77e73b0138eba365f8e8d8030cc7ac to your computer and use it in GitHub Desktop.
Save demndevel/be77e73b0138eba365f8e8d8030cc7ac to your computer and use it in GitHub Desktop.
Sanitize filename c sharp
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