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

余生颓废 提交于 2021-02-07 05:39: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 name is valid, and I'm looking for an easy way to do it without re-inventing the wheel. I'm working in straight C, but targeting the Win32 API.

If there's no such function built-in, how would I go about writing my own? Is there a general algorithm or pattern that Windows follows for determining file name validity?


回答1:


The problem is not so simple, because it depends from what you consider a "valid file name".

The Windows APIs used with UNC paths will let you happily create a lot of names that are deemed invalid inside normal paths, since with the prefix \\?\ you are telling to the Windows APIs to just deliver the path to the filesystem driver, without performing any check; the filesystems themselves often do not really care about what it's used as a file name, once they know that some string is only the file name (i.e. the path/name split has already been done) they generally treat it just as an opaque sequence of characters.

On the other hand, if you want to play it safe, you should perform validation according to the rules specified by the MSDN document you already linked for Win32 names; I don't think that any file system is allowed to have more stringent rules than these on file naming. On the other hand, violating such requirements, although can be supported by the kernel itself, often give bad headaches to many "normal" applications that expect to deal with "traditional" Win32 paths.

But, in my opinion, if you have to create the file immediately, the best validation you can do is to try to actually create/open the file, letting the OS do such work for you, and be prepared to handle gracefully a failure (GetLastError should return ERROR_BAD_PATHNAME). This will check any other restriction you have on creating such file, e.g. that your application has the appropriate permissions, that the path is not on a readonly medium, ...

If, for some reason, this is not possible, you may like the shell function PathCleanupSpec: provided the requested file name and the directory in the file system where it has to be created, this function will remove all the invalid characters (I'm not sure about reserved DOS names, they are not listed in its documentation) making the path "probably valid" and notifying you if any modification was made (so you can use it also only for validation).

Notice that this function is marked as "modifiable or removable in any future Windows version", although Microsoft policy is generally that "anything that made it way to a public header will remain public forever".




回答2:


In case you are checking if the file name is valid in the sense "can the file be named like this?" :

No, there is no function to directly check that. You will have to write you own function.

But, if you know what is a valid file name (the valid file name does now contain any of the following: \ / : * ? " < > |) that shouldn't be such a problem.

You could perhaps help your self with some of these functions from ctype.h (with them you can check if a specific character belongs to some specific character classes):

http://www.cplusplus.com/reference/clibrary/cctype/




回答3:


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,...



来源:https://stackoverflow.com/questions/7130885/how-can-i-validate-if-a-file-is-name-valid-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!