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

后端 未结 27 1705
-上瘾入骨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条回答
  •  [愿得一人]
    2020-11-22 10:37

    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.

提交回复
热议问题