Mega Code Archive

 
Categories / C# / File Stream
 

Removes invalid charactes from filenames, like the slash and backslash

//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers.  //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util {     /// <summary>     /// Class with handy stirng routines     /// </summary>     public class StringUtil     {         /// <summary>         /// Removes leading and trailing quotes "          /// </summary>         /// <param name="filename"></param>         /// <returns></returns>         public static string StripQuotes(string filename)         {             return filename == null ? null : filename.Trim('"');         }         /// <summary>         /// removes invalid charactes from filenames, like the slash and backslash         /// </summary>         /// <param name="fileName"></param>         /// <returns></returns>         public static string RemoveInvalidCharactersFromFileName(string fileName)         {             return Strip(fileName, "\\", "/", ":", "*", "?", "\"", "<", ">", "|");         }         /// <summary>         /// removes the specified strings in the string array from the input string         /// </summary>         /// <param name="input"></param>         /// <param name="removals"></param>         /// <returns></returns>         public static string Strip(string input, params string[] removals)         {             string revised = input;             foreach (string removal in removals)             {                 int i;                 while ((i = revised.IndexOf(removal)) > -1)                     revised = revised.Remove(i, removal.Length);             }             return revised;         }    } }