How to remove illegal characters from path and filenames?

Posted by Gary Willoughby on Stack Overflow See other posts from Stack Overflow or by Gary Willoughby
Published on 2008-09-28T15:52:10Z Indexed on 2010/04/08 12:53 UTC
Read the original article Hit count: 1091

Filed under:
|
|
|

I need a robust and simple way to remove illegal path and file characters from a simple string. I've used the below code but it doesn't seem to do anything, what am i missing?

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
    	static void Main(string[] args)
    	{
    		string illegal = "\"M<>\"\\a/ry/ h**ad:>> a\\/:*?\"<>| li*tt|le|| la\"mb.?";

    		illegal = illegal.Trim(Path.GetInvalidFileNameChars());
    		illegal = illegal.Trim(Path.GetInvalidPathChars());

    		Console.WriteLine(illegal);
    		Console.ReadLine();
    	}
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about directory