简体   繁体   English

使用C ++获取Windows文件信息

[英]Get a Windows file information using C++

Is there any way to can check for some properties of a Windows file using C++? 有什么方法可以使用C ++检查Windows文件的某些属性? The wanted formation is such as last access date, MIME Type, ... 所需的格式,例如最后访问日期,MIME类型,...

For some other information extraction I am using Boost Filesystem. 对于其他一些信息提取,我正在使用Boost Filesystem。

Thanks in advance 提前致谢

For the file modification date, you could use Windows' API directly ( GetFileTime() ) or use boost::filesystem::last_write_time() . 对于文件修改日期,您可以直接使用Windows的API( GetFileTime() )或使用boost::filesystem::last_write_time()

As for MIME type, there's no real API as far as I'm aware. 至于MIME类型,据我所知,还没有真正的API。 You'll essentially have to read the registry key HKEY_CLASSES_ROOT\\.<yourextension> and look for a value Content Type . 您基本上必须阅读注册表项HKEY_CLASSES_ROOT\\.<yourextension>并查找值Content Type If there is one, it includes the MIME type that is set for the extension. 如果存在,则包括为扩展名设置的MIME类型。

The aims of C++ and boost are to be cross-platform. C ++和boost的目标是跨平台。 The language, the standard library and the boost library try to abstract the idiosyncrasies of the various environments from you, so your stated goal is inherently contentious with writing standard C++. 语言,标准库和boost库试图从您那里抽象出各种环境的特质,因此您陈述的目标与编写标准 C ++本质上是有争议的。

However if you know that you'll be compiling and running specifically on Windows you can wrap your code inside 但是,如果您知道要在Windows上专门编译和运行,则可以将代码包装在其中

#ifdef _WIN32
/* code here */
#endif

and call BOOL WINAPI GetFileAttributesEx(...) directly. 并直接调用BOOL WINAPI GetFileAttributesEx(...)

GetFileAttributesEx is part of kernel32.dll, which is loaded into every process, so you should be able to compile and link against it if you've included <windows.h> in your translation unit. GetFileAttributesEx是kernel32.dll的一部分,该文件已加载到每个进程中,因此,如果在翻译单元中包含<windows.h> ,则应该能够对其进行编译和链接。

To get last access time, you can always use stat() , which is available on any OS (and it does not need Boost either). 为了获得上次访问时间,您可以始终使用stat() ,该值在任何操作系统上都可用(并且也不需要Boost)。

As for MIME type, I don't think there is simple way to get that without analyzing file content. 至于MIME类型,我认为没有分析文件内容的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM