Wednesday, April 20, 2011

C# FileName Remove Special Characters not Allowed in FileName

While generating a filename programatically/dynamically by accepting the filename as user input, it is mandatory to make the raw filename valid by removing the special characters if given.

The following are the special characters which are not allowed in filename.
Note: Period(.) is allowed but as filename can't be start or end with it and consecutive periods(.. or ...) are not allowed, considering it as invalid char for filename.
using System;

using System.Linq;

private static string GetValidFileName(string rawFileName)


string fileName = rawFileName;

//special chars not allowed in filename

string specialChars = @"\/:*?""<>|#%&.{}~";

//Replace special chars in raw filename with empty spaces to make it valid

Array.ForEach(specialChars.ToCharArray(), specialChar => fileName = fileName.Replace(specialChar, ' '));

fileName = fileName.Replace(" ", string.Empty);//Recommended to remove the empty spaces in filename

return fileName;


To know more about filename constraints, look at the detailed post here.


  1. You could use Path.GetInvalidPathChars instead of hardcode the characters.

    1. even better -> Path.GetInvalidFileNameChars

    2. That's not sufficient. SharePoint has additional characters, that are not allowed, not mentioned in "InvalidFileNameChars"

      var invalidChars = Path.GetInvalidFileNameChars();

      var additionalInvalidSharePointSpecialCharacters = new char[7] { '~', '#', '%', '&', '{', '}', '+' };

      // the plus sign '+' does not cause an error, but it is mentioned here to be not allowed:

      invalidChars = invalidChars.Concat(additionalInvalidSharePointSpecialCharacters.AsEnumerable()).ToArray();
      var invalidCharsRemoved = fileName.Where(x => !invalidChars.Contains(x)).ToArray();

      After that, you have to remove consecutive dots in the filename and dots at the end and beginning, as mentioned by Murali and in the kb article.

  2. The best options which I have come across and use for a long time are:

    private static string GetValidFileName(string fileName)
    // remove any invalid character from the filename.
    return Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_. ]+", "");

    private static string GetValidFileName(string fileName)
    return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));

    Its explained in this post: