The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,117 other followers

.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

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, "_" );
}

Next to GetInvalidFileNameChars, you have GetInvalidPathChars.

–jeroen

via: validation – C# Sanitize File Name – Stack Overflow.

2 Responses to “.NET/C#: Cleaning up path names with invalid characters (via: validation – C# Sanitize File Name – Stack Overflow)”

  1. Jessie said

    The question was about paths, not filenames, and the invalid characters for these are different.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: