andrew4582

SanitizeFilename

Sep 23rd, 2011
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.55 KB | None | 0 0
  1.     /// <summary>
  2.     /// Sanitizes a filename by removing invalid characters.
  3.     /// </summary>
  4.     /// <param name="path">The path.</param>
  5.     /// <returns>The filename with invalid characters stripped.</returns>
  6.     public static string SanitizeFilename(string path) {
  7.         if(path == null || path.Length == 0) {
  8.             return path;
  9.         }
  10.         string safePath = path;
  11.         Array.ForEach(System.IO.Path.GetInvalidFileNameChars(), c => safePath = safePath.Replace(c.ToString(), string.Empty));
  12.         return safePath;
  13.     }
Advertisement
Add Comment
Please, Sign In to add comment