How do I check if a given string is a legal/valid file name under Windows?

后端 未结 27 1391
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:38

I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I n

27条回答
  •  -上瘾入骨i
    2020-11-22 10:30

    This class cleans filenames and paths; use it like

    var myCleanPath = PathSanitizer.SanitizeFilename(myBadPath, ' ');
    

    Here's the code;

    /// 
    /// Cleans paths of invalid characters.
    /// 
    public static class PathSanitizer
    {
        /// 
        /// The set of invalid filename characters, kept sorted for fast binary search
        /// 
        private readonly static char[] invalidFilenameChars;
        /// 
        /// The set of invalid path characters, kept sorted for fast binary search
        /// 
        private readonly static char[] invalidPathChars;
    
        static PathSanitizer()
        {
            // set up the two arrays -- sorted once for speed.
            invalidFilenameChars = System.IO.Path.GetInvalidFileNameChars();
            invalidPathChars = System.IO.Path.GetInvalidPathChars();
            Array.Sort(invalidFilenameChars);
            Array.Sort(invalidPathChars);
    
        }
    
        /// 
        /// Cleans a filename of invalid characters
        /// 
        /// the string to clean
        /// the character which replaces bad characters
        /// 
        public static string SanitizeFilename(string input, char errorChar)
        {
            return Sanitize(input, invalidFilenameChars, errorChar);
        }
    
        /// 
        /// Cleans a path of invalid characters
        /// 
        /// the string to clean
        /// the character which replaces bad characters
        /// 
        public static string SanitizePath(string input, char errorChar)
        {
            return Sanitize(input, invalidPathChars, errorChar);
        }
    
        /// 
        /// Cleans a string of invalid characters.
        /// 
        /// 
        /// 
        /// 
        /// 
        private static string Sanitize(string input, char[] invalidChars, char errorChar)
        {
            // null always sanitizes to null
            if (input == null) { return null; }
            StringBuilder result = new StringBuilder();
            foreach (var characterToTest in input)
            {
                // we binary search for the character in the invalid set. This should be lightning fast.
                if (Array.BinarySearch(invalidChars, characterToTest) >= 0)
                {
                    // we found the character in the array of 
                    result.Append(errorChar);
                }
                else
                {
                    // the character was not found in invalid, so it is valid.
                    result.Append(characterToTest);
                }
            }
    
            // we're done.
            return result.ToString();
        }
    
    }
    

提交回复
热议问题