当前位置 : 主页 > 编程语言 > c语言 >

c# 剔除 文件名中的非法字符 替换为同样式的其他字符

来源:互联网 收集:自由互联 发布时间:2021-06-25
/// 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 2 3 4 5 6 7 8 9 10 11 12
网友评论