Click here to Skip to main content
15,799,363 members
Articles / Programming Languages / C#

Removing characters which are not allowed in Windows filenames

Rate me:
Please Sign up or sign in to vote.
4.93/5 (20 votes)
13 Apr 2014CPOL 87.9K   26   24
Sometimes, I need to create files or folders directly, and use existing data to provide the file name - and then my app throws an exception because there are "illegal characters in the file name" - so this is a simple way to remove them.


It's easy to remove a characater from a string in c#:

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

Will do it.'s kinda clumsy to repeat that for all the illegal characters in a filename - not to mention wasteful, since it creates a new string for each character you try to remove. Why can't you just go:

MyString = myString.RemoveAll(@"\/:*?""<>|");

Well...because the method doesn't exist...:laugh:

Using the code

A little regex makes it all so simple:

Regex illegalInFileName = new Regex(@"[\\/:*?""<>|]");
string myString = illegalInFileName.Replace(myString, "");

All done!

Or better (though a little less readable):

private Regex illegalInFileName = new Regex(string.Format("[{0}]", Regex.Escape(new string(Path.GetInvalidFileNameChars()))), RegexOptions.Compiled);
string myString = @"A\\B/C:D?E*F""G<H>I|";
myString = illegalInFileName.Replace(myString, "");

This method suggested by Michael_Davies[^] and for which I am most grateful!


2014 Apr 14 Original version.

2014 Apr 14 Addition of a technically better version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

SuggestionAvoiding Cthulhu Pin
Richard Deeming24-Apr-14 5:10
mveRichard Deeming24-Apr-14 5:10 
GeneralWhat .NET thinks invalid characters are Pin
Jon Hayward14-Apr-14 21:28
Jon Hayward14-Apr-14 21:28 
Question4, because..... Pin
Kevin Marois14-Apr-14 8:39
professionalKevin Marois14-Apr-14 8:39 
AnswerRe: 4, because..... Pin
OriginalGriff14-Apr-14 9:03
mvaOriginalGriff14-Apr-14 9:03 
GeneralMy vote of 3 Pin
Member 1004637414-Apr-14 8:31
Member 1004637414-Apr-14 8:31 
GeneralRe: My vote of 3 Pin
OriginalGriff14-Apr-14 8:39
mvaOriginalGriff14-Apr-14 8:39 
GeneralCompiled or Not Compiled and the advantage Pin
Michael_Davies14-Apr-14 7:20
Michael_Davies14-Apr-14 7:20 
GeneralNice :-) Pin
hypermellow14-Apr-14 6:06
professionalhypermellow14-Apr-14 6:06 
QuestionTake your good idea, and you should consider adding "RemoveAll" as a String Extension method....<eom> Pin
Jason Vogel14-Apr-14 4:01
Jason Vogel14-Apr-14 4:01 
QuestionNitpicking Pin
BotCar13-Apr-14 23:21
BotCar13-Apr-14 23:21 
AnswerRe: Nitpicking Pin
OriginalGriff13-Apr-14 23:48
mvaOriginalGriff13-Apr-14 23:48 
GeneralRe: Nitpicking Pin
BotCar14-Apr-14 0:07
BotCar14-Apr-14 0:07 
GeneralMy vote of 5 Pin
Rajesh Buddaraju13-Apr-14 21:12
Rajesh Buddaraju13-Apr-14 21:12 
GeneralLiked It Pin
Rajneesh Rai13-Apr-14 20:35
Rajneesh Rai13-Apr-14 20:35 
QuestionVery Nice, Pin
Rahul VB13-Apr-14 18:33
professionalRahul VB13-Apr-14 18:33 
QuestionGood stuff! Pin
Volynsky Alex13-Apr-14 11:07
professionalVolynsky Alex13-Apr-14 11:07 
SuggestionImprovement Pin
Ravi Bhavnani13-Apr-14 8:52
professionalRavi Bhavnani13-Apr-14 8:52 
GeneralRe: Improvement Pin
OriginalGriff13-Apr-14 23:55
mvaOriginalGriff13-Apr-14 23:55 
GeneralRe: Improvement Pin
Ravi Bhavnani14-Apr-14 2:30
professionalRavi Bhavnani14-Apr-14 2:30 
QuestionGetting Invalid Characters Pin
Michael_Davies13-Apr-14 5:19
Michael_Davies13-Apr-14 5:19 
AnswerRe: Getting Invalid Characters Pin
OriginalGriff13-Apr-14 5:38
mvaOriginalGriff13-Apr-14 5:38 
AnswerRe: Getting Invalid Characters Pin
OriginalGriff13-Apr-14 5:50
mvaOriginalGriff13-Apr-14 5:50 
GeneralHandy! Pin
DaveX8613-Apr-14 3:14
DaveX8613-Apr-14 3:14 
GeneralRe: Handy! Pin
OriginalGriff13-Apr-14 3:36
mvaOriginalGriff13-Apr-14 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.