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

相关推荐
new_zhou2 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序
Rocket MAN3 小时前
Rovo Dev CLI Windows 安装与使用指南
windows
fzyz1236 小时前
Windows系统下WSL从C盘迁移方案
人工智能·windows·深度学习·wsl
csdn_aspnet8 小时前
在 Windows 机器上安装和配置 RabbitMQ
windows·rabbitmq
csdn_aspnet9 小时前
Windows Server 上的 RabbitMQ 安装和配置
windows·rabbitmq
热爱生活的猴子13 小时前
Poetry 在 Linux 和 Windows 系统中的安装步骤
linux·运维·windows
R-sz16 小时前
java流式计算 获取全量树形数据,非懒加载树,递归找儿
java·开发语言·windows
柳鲲鹏1 天前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
专注VB编程开发20年1 天前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
李洋-蛟龙腾飞公司1 天前
HarmonyOS NEXT应用元服务常见列表操作分组吸顶场景
linux·运维·windows