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

相关推荐
x***r15112 小时前
SuperScan4单文件扫描安装步骤详解(附端口扫描与主机存活检测教程)
windows
不爱学习的老登13 小时前
Windows客户端与Linux服务器配置ssh无密码登录
linux·服务器·windows
陌陌龙14 小时前
全免去水印大师 v1.7.6 | 安卓端高效水印处理神器
windows
csdn2015_15 小时前
将object转换成list
开发语言·windows·python
SJjiemo18 小时前
LeafView 图片查看器
windows
ENEN988119 小时前
【精品珍藏自购付费资源】2026年日历PSD模板合集【PSD CDR多格式可编辑】已分类含预览 [7.5G]
windows·经验分享·电脑
njsgcs20 小时前
windows ui窗口post 我wsl开放的flask llm端点
windows·ui·flask·post
这波不该贪内存的21 小时前
数据结构三个典型的类型整理
数据结构·windows
love530love21 小时前
突破 Windows 编译禁区:BitNet 1-bit LLM 推理框架 GPU 加速部署编译 BitNet CUDA 算子全记录
c++·人工智能·pytorch·windows·python·cuda·bitnet
咚咚王者21 小时前
人工智能之视觉领域 计算机视觉 第二章 环境搭建(Windows/Mac/Linux通用)
人工智能·windows·计算机视觉