Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Tip/Trick

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 90.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.

Introduction

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

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

Will do it. But...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:

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

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

Using the code

A little regex makes it all so simple:

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

All done!

Or better (though a little less readable):

C#
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!

History

2014 Apr 14 Original version.

2014 Apr 14 Addition of a technically better version.

License

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


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