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("|", "");
*/
#endregion
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.
Remember Me
Page rendered at Sunday, August 01, 2010 9:30:43 AM (South Africa Standard Time, UTC+02:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.