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


        /// <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;
        }
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);
posted @ 2019-10-21 22:47  ToLing·  阅读(965)  评论(0编辑  收藏  举报