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

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 need to check if it's going to be a legal filename under Windows. I've tried to use regular expression like [a-zA-Z0-9_]+ but it doesn't include many national-specific characters from various languages (eg umlauts and so on). What is the best way to do such a check?


You can get a list of invalid characters from Path.GetInvalidPathChars and GetInvalidFileNameChars .

UPD: See Steve Cooper's suggestion on how to use these in a regular expression.

UPD2: Note that according to the Remarks section in MSDN "The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names." The answer provided by sixlettervaliables goes into more details.


From MSDN's "Naming a File or Directory," here are the general conventions for what a legal file name is under Windows:

You may use any character in the current code page (Unicode/ANSI above 127), except:

  • < > : " / | ? *
  • Characters whose integer representations are 0-31 (less than ASCII space)
  • Any other character that the target file system does not allow (say, trailing periods or spaces)
  • Any of the DOS names: CON, PRN, AUX, NUL, COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9 (and avoid AUX.txt, etc)
  • The file name is all periods
  • Some optional things to check:

  • File paths (including the file name) may not have more than 260 characters (that don't use the ? prefix)
  • Unicode file paths (including the file name) with more than 32,000 characters when using ? (note that prefix may expand directory components and cause it to overflow the 32,000 limit)

  • For .Net Frameworks prior to 3.5 this should work:

    Regular expression matching should get you some of the way. Here's a snippet using the System.IO.Path.InvalidPathChars constant;

    bool IsValidFilename(string testName)
    {
        Regex containsABadCharacter = new Regex("[" 
              + Regex.Escape(System.IO.Path.InvalidPathChars) + "]");
        if (containsABadCharacter.IsMatch(testName)) { return false; };
    
        // other checks for UNC, drive-path format, etc
    
        return true;
    }
    

    For .Net Frameworks after 3.0 this should work:

    http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars(v=vs.90).aspx

    Regular expression matching should get you some of the way. Here's a snippet using the System.IO.Path.GetInvalidPathChars() constant;

    bool IsValidFilename(string testName)
    {
        Regex containsABadCharacter = new Regex("["
              + Regex.Escape(new string(System.IO.Path.GetInvalidPathChars())) + "]");
        if (containsABadCharacter.IsMatch(testName)) { return false; };
    
        // other checks for UNC, drive-path format, etc
    
        return true;
    }
    

    Once you know that, you should also check for different formats, eg c:mydrive and serversharedirfile.ext

    链接地址: http://www.djcxy.com/p/57126.html

    上一篇: 如何在Bash中将字符串从大写转换为小写?

    下一篇: 如何在Windows下检查给定的字符串是否为合法/有效的文件名?