文件夹过滤非法字符

 1 private static string RemoveInvalidFileNameChars(string fileName)
 2         {
 3             if (string.IsNullOrWhiteSpace(fileName)) return null;
 4 
 5             string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
 6             StringBuilder sb = new StringBuilder();
 7             //foreach (char c in invalid) {fileName = fileName.Replace(c.ToString(), "");}
 8             foreach (char c in fileName)
 9             {
10                 if (!invalid.Contains(c)) sb.Append(c);
11             }
12             return sb.ToString();
13         }

 

posted @ 2022-04-22 22:36  无定的界  阅读(89)  评论(0)    收藏  举报