本文介绍了如何验证文件在Windows中是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有我可以传递字符串值的Windows API函数,该函数将返回一个指示文件名是否有效的值?

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?

我需要验证文件名是否有效,并且我正在寻找一种简便的方法来完成该工作,而无需重新发明轮子.我使用的是纯C语言,但是针对Win32 API.

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.

如果没有内置这样的函数,我将如何编写自己的函数?Windows是否使用通用的算法或模式来确定文件名的有效性?

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?

推荐答案

问题不是那么简单,因为它取决于您认为的有效文件名".

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

与UNC路径一起使用的Windows API使您可以愉快地创建许多在常规路径内被视为无效的名称,因为使用前缀 \\?\ 告诉Windows API,仅将路径传递到文件系统驱动程序,而不执行任何检查;文件系统本身通常并不真的在乎它用作文件名,一旦他们知道某个字符串只是文件名(即已完成路径/名称拆分),它们通常会将其视为不透明的文件名序列.字符.

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.

另一方面,如果您想安全使用,则应根据您已经为Win32名称链接的MSDN文档;我不认为任何文件系统都可以拥有比文件命名规则更严格的规则.另一方面,违反这些要求,尽管可以由内核本身来支持,但常常使许多希望处理传统" Win32路径的正常"应用程序感到头痛.

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.

但是,我认为,如果您必须立即创建文件,则最好的验证方法是尝试实际创建/打开文件,让操作系统为您完成此类工作,并准备好妥善处理失败( GetLastError 应该返回 ERROR_BAD_PATHNAME ).这将检查您对创建此类文件的其他任何限制,例如您的应用程序具有适当的权限,该路径不在只读介质上,...

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

如果由于某种原因这是不可能的,则您可能会喜欢shell函数 PathCleanupSpec :提供了请求的文件名和必须在其中创建文件系统的目录,此函数将删除所有无效字符(我不确定保留的DOS名称,它们没有在其文档中列出),使路径可能有效"并通知您是否进行了任何修改(因此您也可以仅将其用于验证).

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

请注意,此功能被标记为在任何将来的Windows版本中都是可修改或可移动的",尽管Microsoft的政策通常是任何成为公共标头的内容将永远保持公开状态".

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

这篇关于如何验证文件在Windows中是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:21
查看更多