.NET/C#: Cleaning up path names with invalid characters (via: validation – C# Sanitize File Name – Stack Overflow)
Posted by jpluimers on 2012/11/13
Thanks Andre for this cleanup code:
To clean up a file name you could do this
[sourcecode language=”csharp”]
private static string MakeValidFileName(string name)
{
string invalidChars = Regex.Escape( new string( Path.GetInvalidFileNameChars() ) );
string invalidReStr = string.Format( @"[{0}]+", invalidChars );
return Regex.Replace( name, invalidReStr, "_" );
}
[/sourcecode]
Next to GetInvalidFileNameChars, you have GetInvalidPathChars.
–jeroen






Jessie said
The question was about paths, not filenames, and the invalid characters for these are different.
jpluimers said
That’s why I name two functions (;