Wednesday, June 6, 2007

I had to write a quick method for removing illegal characters from a Windows filename. Wanting to do this in a non standard approach I decided to try out String.Replace() methods overload which accepts a character array as apposed to a string.


We get the list of illegal characters and put them into a character array.

We then loop through each illegal character in the array and replace any instances with a null character.

Finally we do a string replace at the end to clean up all the null characters.


Here is the code:


        #region filter any ilegal filename characters -->  /\<>:"'|


  string sFileName = "myfile<>\\/:\"|.zip";       


        // define ilegal characters

        char[] cFilterArray = new char[7];

        cFilterArray[0] = '/';

        cFilterArray[1] = '\\';

        cFilterArray[2] = '<';

        cFilterArray[3] = '>';

        cFilterArray[4] = ':';

        cFilterArray[5] = '"';

        cFilterArray[6] = '|';


  // loop through and replace each invalid character with null char

        foreach (char c in cFilterArray)


          sFileName = sFileName.Replace(c, char.MinValue);



        // remove null characters

        sFileName = sFileName.Replace("\0", "");


  /* traditional method of using string.Replace method

  sFileName = sFileName.Replace("/", "");

        sFileName = sFileName.Replace("\\", "");

        sFileName = sFileName.Replace("<", "");

        sFileName = sFileName.Replace(">", "");

        sFileName = sFileName.Replace(":", "");

        sFileName = sFileName.Replace("\"", "");

        sFileName = sFileName.Replace("|", "");





It’s important to note here that char.MinValue is actually equal to “\0”. As such every time I replace an illegal character, I’m actually replacing it with a null char. Hence why at the end we are forced to use the String.Replace() method to remove all the null char’s.

Update:  18 August 2011

As this post receives thousands of new visitors each year, I felt it necessary to provide a more up to date example for removing ilegal characters.

The code below will work in all .Net 2.0+ and requires the System.IO reference.

      string sFilename = "file!@#$^&*()<>:/?~.txt";

      foreach (var invalidChar in Path.GetInvalidFileNameChars())
        sFilename = sFilename.Replace(invalidChar.ToString(), string.Empty);


Here is the Linq equivalent of the code above.

      string sFilename = "file!@#$^&*()<>:/?~.txt";
      sFilename = Path.GetInvalidFileNameChars().Aggregate(sFilename, (current, invalidChar) => current.Replace(invalidChar.ToString(), string.Empty));