How can I validate if a file is name valid in Windows?

前端 未结 3 1466
囚心锁ツ
囚心锁ツ 2021-02-20 11:23

Is there a Windows API function that I can pass a string value to that will return a value indicating whether a file name is valid or not?

I need to verify that a file n

3条回答
  •  猫巷女王i
    2021-02-20 11:56

    This function gives you the list of invalid chars for a filename. Up to you to check that your filename doesn't contain any:

    public static char[] Path.GetInvalidFileNameChars()
    

    Docs here.

    Note that if you want to validate a directory name, you should use GetInvalidPathChars().

    EDIT: Oooops! Sorry, I thought you were on .NET. Using Reflector, here's what this functions boils down to:

    '"', '<', '>', '|', 
    '\0', '\x0001', '\x0002', '\x0003', '\x0004', '\x0005', '\x0006', 
    '\a', '\b', '\t', '\n', '\v', '\f', '\r', 
    '\x000e', '\x000f', '\x0010', '\x0011', '\x0012', '\x0013', '\x0014', '\x0015', 
    '\x0016', '\x0017', '\x0018', '\x0019', '\x001a', '\x001b', '\x001c', '\x001d', 
    '\x001e', '\x001f', 
    ':', '*', '?', '\\', '/'
    

    Note that, in addition, there are reserved names such as prn, con, com1, com2,... , lpt1, lpt2,...

提交回复
热议问题