public static string CleanFolderName(string name, bool replace = true, ColonReplacementFormat colonReplacement = ColonReplacementFormat.Delete) { name = FileNameCleanupRegex.Replace(name, match => match.Captures[0].Value[0].ToString()); name = name.Trim(' ', '.'); return(CleanFileName(name, replace, colonReplacement)); }
public static string CleanFileName(string name, bool replace = true, ColonReplacementFormat colonReplacement = ColonReplacementFormat.Delete) { var colonReplacementFormat = colonReplacement.GetFormatString(); string result = name; string[] badCharacters = { "\\", "/", "<", ">", "?", "*", ":", "|", "\"" }; string[] goodCharacters = { "+", "+", "", "", "!", "-", colonReplacementFormat, "", "" }; for (int i = 0; i < badCharacters.Length; i++) { result = result.Replace(badCharacters[i], replace ? goodCharacters[i] : string.Empty); } return(result.Trim()); }
public static String GetFormatString(this ColonReplacementFormat format) { switch (format) { case ColonReplacementFormat.Delete: return(""); case ColonReplacementFormat.Dash: return("-"); case ColonReplacementFormat.SpaceDash: return(" -"); case ColonReplacementFormat.SpaceDashSpace: return(" - "); default: return(""); } }