How to remove illegal characters from path and filenames?

前端 未结 29 3371
离开以前
离开以前 2020-11-22 17:18

I need a robust and simple way to remove illegal path and file characters from a simple string. I\'ve used the below code but it doesn\'t seem to do anything, what am I miss

29条回答
  •  萌比男神i
    2020-11-22 17:24

    I wrote this monster for fun, it lets you roundtrip:

    public static class FileUtility
    {
        private const char PrefixChar = '%';
        private static readonly int MaxLength;
        private static readonly Dictionary Illegals;
        static FileUtility()
        {
            List illegal = new List { PrefixChar };
            illegal.AddRange(Path.GetInvalidFileNameChars());
            MaxLength = illegal.Select(x => ((int)x).ToString().Length).Max();
            Illegals = illegal.ToDictionary(x => x, x => ((int)x).ToString("D" + MaxLength).ToCharArray());
        }
    
        public static string FilenameEncode(string s)
        {
            var builder = new StringBuilder();
            char[] replacement;
            using (var reader = new StringReader(s))
            {
                while (true)
                {
                    int read = reader.Read();
                    if (read == -1)
                        break;
                    char c = (char)read;
                    if(Illegals.TryGetValue(c,out replacement))
                    {
                        builder.Append(PrefixChar);
                        builder.Append(replacement);
                    }
                    else
                    {
                        builder.Append(c);
                    }
                }
            }
            return builder.ToString();
        }
    
        public static string FilenameDecode(string s)
        {
            var builder = new StringBuilder();
            char[] buffer = new char[MaxLength];
            using (var reader = new StringReader(s))
            {
                while (true)
                {
                    int read = reader.Read();
                    if (read == -1)
                        break;
                    char c = (char)read;
                    if (c == PrefixChar)
                    {
                        reader.Read(buffer, 0, MaxLength);
                        var encoded =(char) ParseCharArray(buffer);
                        builder.Append(encoded);
                    }
                    else
                    {
                        builder.Append(c);
                    }
                }
            }
            return builder.ToString();
        }
    
        public static int ParseCharArray(char[] buffer)
        {
            int result = 0;
            foreach (char t in buffer)
            {
                int digit = t - '0';
                if ((digit < 0) || (digit > 9))
                {
                    throw new ArgumentException("Input string was not in the correct format");
                }
                result *= 10;
                result += digit;
            }
            return result;
        }
    }
    

提交回复
热议问题