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("|", "│");
}
///
1
1
/// 替换文件名中的非法字符为同样式的其他字符
< p>
2
2
///
3
3
/// name="name">
4
4
///
5
5
public string ReplaceFileName(string name)
6
6
{
7
7
name = name.Replace("/", "/");
8
8
name = name.Replace("\\", "\");
9
9
name = name.Replace(":", ":");
10
10
name = name.Replace("*", "※");
11
11
name = name.Replace("?", "? ");
12
12
name = name.Replace("\"", "“");
13
13
name = name.Replace("<", "<");
14
14
name = name.Replace(">", ">");
15
15
name = name.Replace("|", "│");
16
16
return
17
17
< span role="presentation" style="padding-right: 0.1px;"> name;
18
18
}
19
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);
string strFileName= "文件名称";
StringBuilder rBuilder = new StringBuilder(strFileName);
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
StringBuilder rBuilder = new StringBuilder(rPath);
foreach(char rInvalidChar in Path.GetInvalidPathChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
string strFileName= "文件名称";
StringBuilder rBuilder = new StringBuilder(strFileName);
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
StringBuilder rBuilder = new StringBuilder(rPath);
foreach(char rInvalidChar in Path.GetInvalidPathChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
string strFileName= "文件名称";
StringBuilder rBuilder = new StringBuilder(strFileName);
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
StringBuilder rBuilder = new StringBuilder(rPath);
foreach(char rInvalidChar in Path.GetInvalidPathChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
string strFileName= "文件名称";
StringBuilder rBuilder = new StringBuilder(strFileName);
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
StringBuilder rBuilder = new StringBuilder(rPath);
foreach(char rInvalidChar in Path.GetInvalidPathChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
string strFileName= "文件名称";
StringBuilder rBuilder = new StringBuilder(strFileName);
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
StringBuilder rBuilder = new StringBuilder(rPath);
foreach(char rInvalidChar in Path.GetInvalidPathChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
12
12
string strFileName= "文件名称";
StringBuilder rBuilder = new StringBuilder(strFileName);
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
StringBuilder rBuilder = new StringBuilder(rPath);
foreach(char rInvalidChar in Path.GetInvalidPathChars())
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
C
1
1
2
2
string strFileName= "文件名称";
3
3
StringBuilder rBuilder = new StringBuilder(strFileName);
4
4
foreach(char rInvalidChar in Path.GetInvalidFileNameChars())
5
5
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
6
6
7
7
C
8
8
string rPath = "路径";
9
9
StringBuilder rBuilder = new StringBuilder(rPath);
10
10
foreach(char rInvalidChar in Path.GetInvalidPathChars())
11
11
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
12
12