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

相关推荐
Yana_Zeng1 小时前
win10安装spark3.1详细流程(小白用)
hadoop·windows·spark
sukalot2 小时前
windows显示驱动开发-浮点、围栏支持、资源管理
windows·驱动开发
Hello.Reader3 小时前
Flink State V2 实战从同步到异步的跃迁
网络·windows·flink
皓月盈江6 小时前
Windows系统如何批量添加防火墙策略禁止端口入和出?
windows·netsh·批量添加防火墙策略·禁止端口入和出
EnCi Zheng16 小时前
SpringBoot + PostgreSQL 密码认证失败 Windows 系统解决方案
windows·spring boot·postgresql
Damon小智18 小时前
玩转CodeX:CodeX安装教程(Windows+Linux+MacOS)
linux·windows·macos·ai·ai编程·codex·gpt-5
用户311879455921818 小时前
DOpusInstall-13.2.exe 安装方法,简单几步完成
windows
张某人的胡思乱想1 天前
Create/Assemble/Link x64 Windows
windows
ThisIsMirror1 天前
CompletableFuture并行任务超时处理模板
java·windows·python
say_fall1 天前
精通C语言(2.结构体)(内含彩虹)
c语言·开发语言·windows