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库。

相关推荐
keep the15 小时前
Windows启动IOS17/18的WDA
windows·ios·自动化
取个名字太难了a15 小时前
intel白皮书卷2 第二章(AI翻译)
windows
爱编码的傅同学15 小时前
【线程同步】信号量与环形队列的生产消费模型
linux·windows·ubuntu·centos
专注VB编程开发20年16 小时前
如何强制ANY CPU的.net程序按32位或64位模式运行?
windows·.net
yaoxin52112319 小时前
303. Java Stream API - 查找元素
java·windows·python
z.q.xiao21 小时前
【镜像模式】WSL如何访问windows内网服务
linux·网络·windows·gitlab·wsl·dns
gf132111121 小时前
python_生成RPA运行数据报告
windows·python·rpa
FL16238631291 天前
Windows上GPU版本的Paddle Inference3.2.1安装和使用教程
windows·paddle
嘴贱欠吻!1 天前
Kuikly搭建OpenHarmony教程01:源码构建与运行(Windows)
windows
2601_949480061 天前
Flutter for OpenHarmony 音乐播放器App实战 - 主题设置实现
windows·flutter