Windows API遍历桌面上所有文件

要获取桌面上的图标,可以使用Windows API中的Shell API。以下是遍历桌面上所有文件的示例代码:

cpp 复制代码
#include <Windows.h>
#include <ShlObj.h>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    // 获取桌面文件夹的路径
    wchar_t desktopPath[MAX_PATH];
    if (FAILED(SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, desktopPath)))
    {
        cout << "Failed to get desktop folder path." << endl;
        return 1;
    }

    // 获取桌面上的图标
    vector<wstring> icons;

    WIN32_FIND_DATAW findData;
    HANDLE hFind = FindFirstFileW((wstring(desktopPath) + L"\\*").c_str(), &findData);
    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                continue;

            wstring iconPath = wstring(desktopPath) + L"\\" + findData.cFileName;
            icons.push_back(iconPath);
        } while (FindNextFileW(hFind, &findData));

        FindClose(hFind);
    }

    // 输出桌面图标路径
    for (const auto& icon : icons)
    {
        wcout << icon << endl;
    }

    return 0;
}

在这个示例中,通过调用`SHGetFolderPathW`函数来获取桌面文件夹的路径。然后使用`FindFirstFileW`和`FindNextFileW`遍历桌面上的所有文件和文件夹。我们只对文件感兴趣,所以在循环中排除了文件夹。最后,我们将每个图标的路径存储在一个向量中,并将其输出到控制台。

请注意,此示例代码需要链接Shell32.lib库。

相关推荐
勤劳打代码8 分钟前
Flutter 架构指南 —— windows 平台安装拆解
windows·架构
aramae4 小时前
C++11:现代C++的里程碑
c语言·开发语言·c++·windows·git·后端
执行x4 小时前
wsl2安装+桥接模式+固定IP
linux·windows·ubuntu
做萤石二次开发的哈哈5 小时前
如何接入萤石开放平台 ERTC Windows SDK?
windows·音视频·萤石开放平台
霸道流氓气质6 小时前
Windows 本地搭建 CDC 全链路测试环境(Kafka+Canel+binlog+SpringBoot)
windows·spring boot·kafka
郝开6 小时前
Windows 安装 Ostris AI Toolkit
人工智能·windows·toolkit·ai toolkit
疯狂成瘾者1 天前
Java 常见集合方法
java·windows·python
倒流时光三十年1 天前
MyBatis @MapKey 注解详解
windows·mybatis
生活、追梦者1 天前
【Windows】 CMD 黑窗口命令大全
windows·命令行·cmd·运维技巧
阿米亚波1 天前
【C++ STL】std::list
c++·windows·笔记·stl·list