Removing Invalid Characters from Filenames

When you need to dynamically create names of files from text that might contain invalid characters, for example to generate user-friendly filenames that are based on free-form values entered by a user, you need to remove the invalid filename characters before you create the file and save the content (unless you prefer to get error messages during runtime). Such files can then be stored on disk or perhaps in a SharePoint document library. The following C# method may do the trick.

public static string RemoveInvalidFilenameCharacters(string filename)
if (!string.IsNullOrEmpty(filename))
char[] invalidFilenameCharacters =
// Invalid for SharePoint...
// File system...
// Environment variables
new char[] { System.IO.Path.PathSeparator }).ToArray();
foreach (char character in invalidFilenameCharacters)
filename = filename.Replace(
character.ToString(), string.Empty);
return filename;

This code is not likely to win a prize for optimization, but the method does read quite easily, which improves code maintenance. It also serves as a nice example of how LINQ’s Enumerable.Union and Enumerable.ToArray extension methods can be used. (Remember to include the System.Linq namespace.) If you are primarily concerned with file names for use in SharePoint, you might want to read Microsoft’s Knowledge Base article KB905231 for additional checks to include, or read Eric Legault’s post on this topic (he is also from Winnipeg).