Skip to content

Instantly share code, notes, and snippets.

@sin2akshay
Last active August 8, 2018 03:12
Show Gist options
  • Select an option

  • Save sin2akshay/9d1b99b33c1b6f6dee2687f9b1595185 to your computer and use it in GitHub Desktop.

Select an option

Save sin2akshay/9d1b99b33c1b6f6dee2687f9b1595185 to your computer and use it in GitHub Desktop.
Convert String to Valid File name for Windows in C#
private static string MakeValidFileName(string name, char replaceChar)
{
string str_replaceChar = "" + replaceChar;
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, str_replaceChar);
}
@sin2akshay
Copy link
Copy Markdown
Author

DotNetFiddle Link: https://dotnetfiddle.net/vBWaoa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment