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

相关推荐
陈葛杰10 小时前
PE系统安装教程(傻瓜版)
windows
John_ToDebug11 小时前
浏览器内核崩溃深度分析:从 MiniDump 堆栈到 BindOnce UAF 机制(未完待续...)
c++·chrome·windows
一位赵13 小时前
小练2 选择题
linux·运维·windows
学***542313 小时前
12款分区恢复软件: 恢复已删除/丢失的分区
windows
爱内卷的学霸一枚15 小时前
现代微服务架构实践:从设计到部署的深度解析
windows·微服务·架构
程序猿阿伟17 小时前
《Apple Silicon与Windows on ARM:引擎原生构建与模拟层底层运作深度解析》
arm开发·windows
软件资深者17 小时前
游戏组件DirectX修复工具(DirectX Repair)v4.4增强版
windows·游戏·电脑·系统修复
By北阳18 小时前
Windows 系统中 存储信息加载异常的表现,所有数值都显示为 “0000000” 乱码
windows
wukangjupingbb19 小时前
在 Windows 系统上一键部署 **Moltbot**
人工智能·windows·agent
x***r15120 小时前
maxima-5.47.0-win64数学计算软件安装步骤详解(附数学计算与绘图入门)
windows