Coder Perfect

Sanitize the file name in C#


I’ve been transferring MP3s from several locations into a repository recently. I was utilizing the ID3 tags to create the new file names (thanks, TagLib-Sharp! ), and I realized that I was getting a System. NotSupportedException:

Either File.Copy() or Directory generated this. CreateDirectory().

It didn’t take long for me to recognize that I needed to sanitize my file names. As a result, I did the obvious:

public static string SanitizePath_(string path, char replaceChar)
    string dir = Path.GetDirectoryName(path);
    foreach (char c in Path.GetInvalidPathChars())
        dir = dir.Replace(c, replaceChar);

    string name = Path.GetFileName(path);
    foreach (char c in Path.GetInvalidFileNameChars())
        name = name.Replace(c, replaceChar);

    return dir + name;

Surprisingly, I continued to receive exceptions. It turned out that ‘:’ isn’t part of the Path set. Because it is valid in a path root, GetInvalidPathChars() is legitimate. That makes sense, but this has to be a rather typical occurrence. Is there a tiny piece of code that sanitizes a path? This is the most extensive I’ve come up with, but it feels like overkill.

    // replaces invalid characters with replaceChar
    public static string SanitizePath(string path, char replaceChar)
        // construct a list of characters that can't show up in filenames.
        // need to do this because ":" is not in InvalidPathChars
        if (_BadChars == null)
            _BadChars = new List<char>(Path.GetInvalidFileNameChars());
            _BadChars = Utility.GetUnique<char>(_BadChars);

        // remove root
        string root = Path.GetPathRoot(path);
        path = path.Remove(0, root.Length);

        // split on the directory separator character. Need to do this
        // because the separator is not valid in a filename.
        List<string> parts = new List<string>(path.Split(new char[]{Path.DirectorySeparatorChar}));

        // check each part to make sure it is valid.
        for (int i = 0; i < parts.Count; i++)
            string part = parts[i];
            foreach (char c in _BadChars)
                part = part.Replace(c, replaceChar);
            parts[i] = part;

        return root + Utility.Join(parts, Path.DirectorySeparatorChar.ToString());

Any changes that make this function more efficient and less baroque would be greatly appreciated.

Asked by Jason Sundram

Solution #1

You could do this to tidy up a file name.

private static string MakeValidFileName( string name )
   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, "_" );

Answered by Andre

Solution #2

A shorter solution:

var invalids = System.IO.Path.GetInvalidFileNameChars();
var newName = String.Join("_", origFileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries) ).TrimEnd('.');

Answered by DenNukem

Solution #3

I created this version based on Andre’s wonderful response, but with Spud’s note on reserved terms in mind:

/// <summary>
/// Strip illegal chars and reserved words from a candidate filename (should not include the directory path)
/// </summary>
/// <remarks>
/// </remarks>
public static string CoerceValidFileName(string filename)
    var invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
    var invalidReStr = string.Format(@"[{0}]+", invalidChars);

    var reservedWords = new []
        "CON", "PRN", "AUX", "CLOCK$", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4",
        "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4",
        "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"

    var sanitisedNamePart = Regex.Replace(filename, invalidReStr, "_");
    foreach (var reservedWord in reservedWords)
        var reservedWordPattern = string.Format("^{0}\\.", reservedWord);
        sanitisedNamePart = Regex.Replace(sanitisedNamePart, reservedWordPattern, "_reservedWord_.", RegexOptions.IgnoreCase);

    return sanitisedNamePart;

And here are the unit tests I’ve written.

public void CoerceValidFileName_SimpleValid()
    var filename = @"thisIsValid.txt";
    var result = PathHelper.CoerceValidFileName(filename);
    Assert.AreEqual(filename, result);

public void CoerceValidFileName_SimpleInvalid()
    var filename = @"thisIsNotValid\3\\_3.txt";
    var result = PathHelper.CoerceValidFileName(filename);
    Assert.AreEqual("thisIsNotValid_3__3.txt", result);

public void CoerceValidFileName_InvalidExtension()
    var filename = @"thisIsNotValid.t\xt";
    var result = PathHelper.CoerceValidFileName(filename);
    Assert.AreEqual("thisIsNotValid.t_xt", result);

public void CoerceValidFileName_KeywordInvalid()
    var filename = "aUx.txt";
    var result = PathHelper.CoerceValidFileName(filename);
    Assert.AreEqual("_reservedWord_.txt", result);

public void CoerceValidFileName_KeywordValid()
    var filename = "auxillary.txt";
    var result = PathHelper.CoerceValidFileName(filename);
    Assert.AreEqual("auxillary.txt", result);

Answered by fiat

Solution #4

string clean = String.Concat(dirty.Split(Path.GetInvalidFileNameChars()));

Answered by data

Solution #5

There are numerous viable options available. For the purpose of completeness, here’s a method that doesn’t use regex and instead relies on LINQ:

var invalids = Path.GetInvalidFileNameChars();
filename = invalids.Aggregate(filename, (current, c) => current.Replace(c, '_'));

It’s also a fairly quick answer 😉

Answered by kappadoky

Post is based on