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

相关推荐
玖釉-7 小时前
[Vulkan 学习之路] 08 - 给图片穿马甲:图像视图 (Image Views)
c++·windows·图形渲染
古城小栈9 小时前
Rust 交叉编译:Windows ====> Linux (musl 静态编译)
linux·windows·rust
玖釉-9 小时前
[Vulkan 学习之路] 09 - 显卡的流水线工厂:图形管线概览 (Graphics Pipeline)
c++·windows·图形渲染
晋人在秦 老K10 小时前
Windows 7还能用!VxKex实现Edge浏览器及现代应用兼容方案
windows·edge·win7系统兼容性修复·api扩展工具·老旧系统运行新软件·dll缺失错误解决·兼容性调试方案
love530love10 小时前
EPGF 新手教程 22教学模板不是压缩包:EPGF 如何设计“可复制、可检查、可回收”的课程模板?
ide·人工智能·windows·python·架构·pycharm·epgf
程序员南飞10 小时前
列表对象排序
windows
玖釉-11 小时前
[Vulkan 学习之路] 02 - 万物起源:创建 Vulkan 实例 (Instance)
c++·windows·图形渲染
博学的轮船Y12 小时前
绕过Windows 11安装限制,Rufus带给你“奇迹”,低配电脑的春天
windows·资讯
seasonsyy12 小时前
3.虚拟机中安装Win7系统遇到问题及解决
windows·操作系统·vmware·虚拟机
水饺编程12 小时前
第4章,[标签 Win32] :获取设备环境句柄的第一个方法
c语言·c++·windows·visual studio