| 分享人 | 阿苏00131 |
| 分享时间 | 2022-12-11 23:55 |
| 最后更新 | 2022-12-11 23:55 |
| 修订版本 | 0 |
| 子程序大小 | 1.3 KB |
| Quicker版本 | 1.36.12.0 |
在 C# 中,你可以使用 Path.GetInvalidFileNameChars 方法来获取所有非法文件名字符,然后遍历这些字符,使用 string.Replace 方法来替换这些字符。例如:
string path = "C:\\my_files\\bad file name.txt";char[] invalidChars = Path.GetInvalidFileNameChars();foreach (char c in invalidChars) { path = path.Replace(c, '_'); }// 此时,path 的值为 "C:\\my_files\\bad_file_name.txt"
你也可以使用正则表达式来实现同样的功能,例如:
string path = "C:\\my_files\\bad file name.txt";string regex = "[" + Regex.Escape(new string(Path.GetInvalidFileNameChars())) + "]";path = Regex.Replace(path, regex, "_"); // 此时,path 的值为 "C:\\my_files\\bad_file_name.txt"
| 修订版本 | 更新时间 | 更新说明 |
|---|---|---|
| 0 | 2022-12-11 23:55 |