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

相关推荐
課代表12 小时前
Windows 文本搜索命令 findstr
windows·正则表达式·命令行·文本·匹配·搜索·findstr
吴声子夜歌12 小时前
Windows——系统配置与管理
windows
p***924812 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
白日做梦Q12 小时前
Linux/Windows系统下:traceroute 与 tracert 路由追踪命令全解析
linux·运维·windows
食品一少年13 小时前
【Day1-3】(Windows版)Open Harmony PC 命令行适配指南环境准备篇(2)
windows
Halo_tjn13 小时前
Java List集合
java·windows·计算机
i***586713 小时前
【RabbitMQ】超详细Windows系统下RabbitMQ的安装配置
windows·分布式·rabbitmq
O***Z61616 小时前
Redis——Windows安装
数据库·windows·redis
y***031717 小时前
如何在Windows系统上安装和配置Node.js及Node版本管理器(nvm)
windows·node.js
qq_3363139318 小时前
java基础-集合进阶
java·开发语言·windows