/// summary /// 替换文件名中的非法字符为同样式的其他字符 /// /summary /// param name="name"/param /// returns/returns public string ReplaceFileName(string name) { name = name.Replace("/", "/"); name = name.Replace("\\"
/// <summary> /// 替换文件名中的非法字符为同样式的其他字符 /// </summary> /// <param name="name"></param> /// <returns></returns> public string ReplaceFileName(string name) { name = name.Replace("/", "/"); name = name.Replace("\\", "\"); name = name.Replace(":", ":"); name = name.Replace("*", "※"); name = name.Replace("?", "?"); name = name.Replace("\"", "“"); name = name.Replace("<", "<"); name = name.Replace(">", ">"); name = name.Replace("|", "│"); return name; }19 1
/// <summary>
2
/// 替换文件名中的非法字符为同样式的其他字符
3
/// </summary>
4
/// <param name="name"></param>
5
/// <returns></returns>
6
public string ReplaceFileName(string name)
7
{
8
name = name.Replace("/", "/");
9
name = name.Replace("\\", "\");
10
name = name.Replace(":", ":");
11
name = name.Replace("*", "※");
12
name = name.Replace("?", "?");
13
name = name.Replace("\"", "“");
14
name = name.Replace("<", "<");
15
name = name.Replace(">", ">");
16
name = name.Replace("|", "│");
17
return
18
name;
19
}
C#快速剔除字符串中不合法的文件名 string strFileName= "文件名称"; StringBuilder rBuilder = new StringBuilder(strFileName); foreach(char rInvalidChar in Path.GetInvalidFileNameChars()) rBuilder.Replace(rInvalidChar.ToString(), string.Empty); C#快速剔除字符串中不合法的文件路径字符 string rPath = "路径"; StringBuilder rBuilder = new StringBuilder(rPath); foreach(char rInvalidChar in Path.GetInvalidPathChars()) rBuilder.Replace(rInvalidChar.ToString(), string.Empty);12 1
C#快速剔除字符串中不合法的文件名
2
3
string strFileName= "文件名称";
4
StringBuilder rBuilder = new StringBuilder(strFileName);
5
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
6
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
7
8
C#快速剔除字符串中不合法的文件路径字符
9
string rPath = "路径";
10
StringBuilder rBuilder = new StringBuilder(rPath);
11
foreach(char rInvalidChar in Path.GetInvalidPathChars())
12
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);