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);
        }

  

posted @ 2021-11-27 09:45  Yu  阅读(263)  评论(0编辑  收藏  举报