Click here to Skip to main content
11,570,906 members (59,975 online)
Click here to Skip to main content

Tagged as

Removing characters which are not allowed in Windows filenames

, 13 Apr 2014 CPOL 11.9K 26
Rate this:
Please Sign up or sign in to vote.
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#:

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:

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

Well...because the method doesn't exist...Laugh | :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!

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)

Share

About the Author

OriginalGriff
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?

You may also be interested in...

Comments and Discussions

 
SuggestionAvoiding Cthulhu Pin
Richard Deeming24-Apr-14 4:10
professionalRichard Deeming24-Apr-14 4:10 
GeneralWhat .NET thinks invalid characters are Pin
Jon Hayward14-Apr-14 20:28
memberJon Hayward14-Apr-14 20:28 
Question4, because..... Pin
Kevin Marois14-Apr-14 7:39
memberKevin Marois14-Apr-14 7:39 
AnswerRe: 4, because..... Pin
OriginalGriff14-Apr-14 8:03
protectorOriginalGriff14-Apr-14 8:03 
GeneralMy vote of 3 Pin
Member 1004637414-Apr-14 7:31
memberMember 1004637414-Apr-14 7:31 
GeneralRe: My vote of 3 Pin
OriginalGriff14-Apr-14 7:39
protectorOriginalGriff14-Apr-14 7:39 
GeneralCompiled or Not Compiled and the advantage Pin
Michael_Davies14-Apr-14 6:20
memberMichael_Davies14-Apr-14 6:20 
GeneralNice :-) Pin
hypermellow14-Apr-14 5:06
memberhypermellow14-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
memberJason Vogel14-Apr-14 3:01 
QuestionNitpicking Pin
BotCar13-Apr-14 22:21
memberBotCar13-Apr-14 22:21 
AnswerRe: Nitpicking Pin
OriginalGriff13-Apr-14 22:48
protectorOriginalGriff13-Apr-14 22:48 
GeneralRe: Nitpicking Pin
BotCar13-Apr-14 23:07
memberBotCar13-Apr-14 23:07 
GeneralMy vote of 5 Pin
Rajesh Varma Buddaraju13-Apr-14 20:12
memberRajesh Varma Buddaraju13-Apr-14 20:12 
GeneralLiked It Pin
Rajneesh Rai13-Apr-14 19:35
memberRajneesh 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
protectorOriginalGriff13-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
memberMichael_Davies13-Apr-14 4:19 
AnswerRe: Getting Invalid Characters Pin
OriginalGriff13-Apr-14 4:38
protectorOriginalGriff13-Apr-14 4:38 
AnswerRe: Getting Invalid Characters Pin
OriginalGriff13-Apr-14 4:50
protectorOriginalGriff13-Apr-14 4:50 
GeneralHandy! Pin
DaveX8613-Apr-14 2:14
memberDaveX8613-Apr-14 2:14 
GeneralRe: Handy! Pin
OriginalGriff13-Apr-14 2:36
protectorOriginalGriff13-Apr-14 2:36 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 13 Apr 2014
Article Copyright 2014 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid