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

相关推荐
手揽回忆怎么睡15 小时前
win11显卡NVIDIA GeForce GTX 1660的ComfyUI_windows_portable_nvidia_cu126的节点包安装不上
windows
AxureMost15 小时前
Windows系统调校 20260324 系统优化工具
windows
NGBQ1213816 小时前
ZBrush-2026.0.0-x64-CN-Portable.exe 全解析:Windows 端专业数字雕刻与绘画软件深度指南
windows·数字雕刻·zbrush
牧天白衣.16 小时前
01-集合高级
windows
残雪飞扬17 小时前
Ubuntu上安装 WinBoat(让linux上运行windows软件)
linux·windows·ubuntu
图灵机z17 小时前
【操作系统】四、进程管理
linux·服务器·网络·windows·macos·centos·risc-v
soldierluo18 小时前
openclaw接入企业微信
服务器·人工智能·windows·企业微信
西柚00118 小时前
Windows 安装 MySQL5.7.30正常情况与异常情况
windows
晨曦蜗牛19 小时前
Windows 上 Claude Code 报错 “requires git-bash“ 的完整解决方案
windows·git·bash
chh56319 小时前
从零开始学习C++ -- 基础知识
开发语言·c++·windows·学习·算法