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

后端 未结 27 1390
-上瘾入骨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:28

    One corner case to keep in mind, which surprised me when I first found out about it: Windows allows leading space characters in file names! For example, the following are all legal, and distinct, file names on Windows (minus the quotes):

    "file.txt"
    " file.txt"
    "  file.txt"
    

    One takeaway from this: Use caution when writing code that trims leading/trailing whitespace from a filename string.

提交回复
热议问题