andrew4582

RemoveInvalidPathChars

Jun 5th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. public static string RemoveInvalidPathChars(string s) {
  2.             if(s == null)
  3.                 return null;
  4.             char[] invalid = Path.GetInvalidPathChars();
  5.             string result = string.Empty;
  6.             for(int i = 0;i < s.Length;i++) {
  7.                 char c = s[i];
  8.                 if(invalid.Contains(c))
  9.                     continue;
  10.                 result += c.ToString();
  11.             }
  12.             return result;
  13.         }
  14.         public static string RemoveInvalidFileNameChars(string s) {
  15.             if(s == null)
  16.                 return null;
  17.             char[] invalid = Path.GetInvalidFileNameChars();
  18.             string result = string.Empty;
  19.             for(int i = 0;i < s.Length;i++) {
  20.                 char c = s[i];
  21.                 if(invalid.Contains(c))
  22.                     continue;
  23.                 result += c.ToString();
  24.             }
  25.             return result;
  26.         }
Advertisement
Add Comment
Please, Sign In to add comment