Last active
August 8, 2018 03:12
-
-
Save sin2akshay/9d1b99b33c1b6f6dee2687f9b1595185 to your computer and use it in GitHub Desktop.
Convert String to Valid File name for Windows in C#
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
| 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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DotNetFiddle Link: https://dotnetfiddle.net/vBWaoa