其他分享
首页 > 其他分享> > Illegal characters in path

Illegal characters in path

作者:互联网

 

 

 

执行ClearInvalidChars后,清除完成!

 

 

 public static string ClearInvalidChars(string fullname) {

            var source = fullname;
            char[] cp = new char[] { '\u002A', '\u003A', '\u003F','\u0022','\u003C', '\u003E', '\u007C' };
            var root = fullname.Substring(0, fullname.IndexOf(":")+1);
            var relative = fullname.Substring(fullname.IndexOf(":")+1, fullname.Length- fullname.IndexOf(":")-1);
            foreach (var c in cp)
            {
                relative = relative.Replace(c.ToString(), "_");
            }
            fullname = root + relative;
            var chars = Path.GetInvalidPathChars();
            foreach (var c in chars)
            {
                fullname = fullname.Replace(c.ToString(), "_");
            }
            chars = Path.GetInvalidFileNameChars();
            var path = Path.GetDirectoryName(fullname);
            var filename = Path.GetFileName(fullname);
            foreach (var c in chars)
            {
                filename = filename.Replace(c.ToString(), "_");
            }
            return Path.Combine(path, filename);
        }

  

标签:Illegal,chars,filename,relative,characters,path,var,Path,fullname
来源: https://www.cnblogs.com/yipeng-yu/p/15610692.html