How to remove illegal characters from path and filenames?


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());

5/5/2010 9:51:53 AM

Accepted Answer

Try something like this instead;

string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
    illegal = illegal.Replace(c.ToString(), ""); 

But I have to agree with the comments, I'd probably try to deal with the source of the illegal paths, rather than try to mangle an illegal path into a legitimate but probably unintended one.

Edit: Or a potentially 'better' solution, using Regex's.

string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
illegal = r.Replace(illegal, "");

Still, the question begs to be asked, why you're doing this in the first place.

12/13/2010 11:34:58 PM

The original question asked to "remove illegal characters":

public string RemoveInvalidChars(string filename)
    return string.Concat(filename.Split(Path.GetInvalidFileNameChars()));

You may instead want to replace them:

public string ReplaceInvalidChars(string filename)
    return string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));    

This answer was on another thread by Ceres, I really like it neat and simple.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow