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

相关推荐
Nebula_g7 小时前
JavaSE加强:Stream流
java·开发语言·windows·stream·javase·流
在角落发呆11 小时前
软件配置备份方法:从手动到自动化的实践指南
windows·安全
lingchen190611 小时前
Win 11 系统语言栏悬浮于桌面上影响操作感受,怎么取消?
windows
梅孔立12 小时前
Windows PowerShell 升级 Pi-Coding-Agent + OpenCode,解决 OpenCode 二进制无效报错
windows
东木月14 小时前
Windows C 盘清理指南:安全删除临时文件、缓存与 Prefetch
c语言·windows·安全
水饺编程15 小时前
第1章,[Win32 章节]:Windows 的方方面面
c语言·c++·windows·visual studio
超级大福宝16 小时前
VS Code Remote-SSH 因 RemoteForward 端口冲突导致连接失败的排查与解决
运维·服务器·windows·vscode·ssh
Vex2une16 小时前
Windows PowerShell 发展史:从命令行工具到自动化核心
windows·自动化运维·命令行·powershell·cmd·脚本语言·对象管道
怎么没有名字注册了啊1 天前
Qt开发:MessageBox怎么调出标题栏那个问号按钮?
windows
李子红了时1 天前
EncFS MP中文版汉化下载(免费文件加密工具)
windows·安全