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

相关推荐
ι:4 小时前
NUC12 Pro 安装 Ubuntu 24.04 LTS 详细教程
linux·windows·ubuntu·nuc
YCOSA20255 小时前
微软宣布将于10月7日举行Windows与Surface发布会,聚焦“Windows、NVIDIA RTX Spark、Surface,以及更广泛PC生态”
windows
Source.Liu5 小时前
【A11】 labelprinter(Tauri 版)新建步骤
windows·rust
Punchline2786 小时前
解决 Windows 环境变量 Path 设置时提示“字符太长”的完整指南
windows·环境变量·path
chushiyunen7 小时前
claude code笔记(二)、claude桌面端(内网不好用)
windows
d_benhua7 小时前
以太网测试步骤
windows
冯一川8 小时前
DeepSeek在Windows系统上部署
windows·python
解道Jdon9 小时前
JDK 27发布:紧凑对象头、G1默认、量子安全TLS
ide·windows·git·svn·eclipse·github·visual studio
fengjianhua122511 小时前
免费PDF转换工具哪个转换效果最好?磨针PDF转换准确率评测
windows
TimberWill1 天前
windows终端分屏设置
windows