C++实现windows上获取文件的属性和高级属性
时间: 2024-05-06 21:18:25 浏览: 15
要获取文件的属性和高级属性,可以使用Windows API中的GetFileAttributes函数和SHCreateItemFromParsingName函数。下面是一个简单的C++示例代码:
```cpp
#include <iostream>
#include <Windows.h>
#include <ShObjIdl.h>
#pragma comment (lib, "Shell32.lib")
int main()
{
// 获取文件属性
DWORD attributes = GetFileAttributes(L"C:\\example.txt");
if (attributes != INVALID_FILE_ATTRIBUTES)
{
std::cout << "文件属性:" << std::endl;
if (attributes & FILE_ATTRIBUTE_ARCHIVE)
std::cout << "存档" << std::endl;
if (attributes & FILE_ATTRIBUTE_COMPRESSED)
std::cout << "压缩" << std::endl;
if (attributes & FILE_ATTRIBUTE_DIRECTORY)
std::cout << "目录" << std::endl;
if (attributes & FILE_ATTRIBUTE_HIDDEN)
std::cout << "隐藏" << std::endl;
if (attributes & FILE_ATTRIBUTE_NORMAL)
std::cout << "正常" << std::endl;
if (attributes & FILE_ATTRIBUTE_READONLY)
std::cout << "只读" << std::endl;
if (attributes & FILE_ATTRIBUTE_REPARSE_POINT)
std::cout << "重解析点" << std::endl;
if (attributes & FILE_ATTRIBUTE_SYSTEM)
std::cout << "系统" << std::endl;
if (attributes & FILE_ATTRIBUTE_TEMPORARY)
std::cout << "临时" << std::endl;
}
// 获取文件高级属性
IShellItem* pItem = NULL;
HRESULT hr = SHCreateItemFromParsingName(L"C:\\example.txt", NULL, IID_PPV_ARGS(&pItem));
if (SUCCEEDED(hr))
{
std::cout << "文件高级属性:" << std::endl;
IPropertyStore* pStore = NULL;
hr = pItem->BindToHandler(NULL, BHID_PropertyStore, IID_PPV_ARGS(&pStore));
if (SUCCEEDED(hr))
{
PROPVARIANT propValue;
hr = pStore->GetValue(PKEY_FileVersion, &propValue);
if (SUCCEEDED(hr))
{
std::cout << "文件版本:" << propValue.pwszVal << std::endl;
PropVariantClear(&propValue);
}
hr = pStore->GetValue(PKEY_FileDescription, &propValue);
if (SUCCEEDED(hr))
{
std::cout << "文件描述:" << propValue.pwszVal << std::endl;
PropVariantClear(&propValue);
}
pStore->Release();
}
pItem->Release();
}
return 0;
}
```
这个示例程序首先使用GetFileAttributes函数获取文件的属性,然后使用SHCreateItemFromParsingName函数获取文件的高级属性。在获取文件的高级属性时,我们使用了Windows Shell中定义的PKEY_FileVersion和PKEY_FileDescription属性。这些属性可以用于获取文件的版本信息和描述信息等。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![001](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)