用C++写个电脑桌面文件管理器
以下是一个基于 C++ 和 Windows API 实现的简易桌面文件管理器核心代码,支持遍历桌面文件、显示文件信息及基础打开功能,需在 Visual Studio 等支持 Windows API 的环境中编译运行:
include <windows.h>
include <shlobj.h>
include
include
include
pragma comment(lib, "shell32.lib") // 链接Shell32库
// 存储文件信息的结构体
struct FileInfo {
std::wstring name; // 文件名
std::wstring path; // 文件完整路径
bool isDirectory; // 是否为文件夹
DWORD fileSize; // 文件大小(字节,文件夹为0)
};
// 1. 获取桌面路径
std::wstring GetDesktopPath() {
WCHAR desktopPath[MAX_PATH] = {0};
// SHGetFolderPath已被弃用,推荐用SHGetKnownFolderPath(需Vista及以上系统)
if (SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, desktopPath) != S_OK) {
MessageBoxW(NULL, L"获取桌面路径失败!", L"错误", MB_ICONERROR);
exit(1);
}
return std::wstring(desktopPath) + L"\"; // 拼接路径分隔符
}
// 2. 遍历桌面文件,获取文件信息
std::vector
std::vector
WIN32_FIND_DATAW findData;
HANDLE hFind = FindFirstFileW((desktopPath + L".").c_str(), &findData); // 遍历所有文件
if (hFind == INVALID_HANDLE_VALUE) {
MessageBoxW(NULL, L"遍历桌面文件失败!", L"错误", MB_ICONERROR);
return fileList;
}
do {
// 跳过当前目录(.)和上级目录(..)
if (wcscmp(findData.cFileName, L".") == 0 || wcscmp(findData.cFileName, L"..") == 0) {
continue;
}
FileInfo info;
info.name = findData.cFileName;
info.path = desktopPath + findData.cFileName;
info.isDirectory = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.fileSize = info.isDirectory ? 0 : findData.nFileSizeLow; // 文件夹大小设为0
fileList.push_back(info);
} while (FindNextFileW(hFind, &findData)); // 继续查找下一个文件
FindClose(hFind); // 关闭查找句柄
return fileList;
}
// 3. 显示文件列表(控制台输出)
void ShowFileList(const std::vector
system("cls"); // 清空控制台
std::wcout << L"==================== 桌面文件管理器 ====================\n";
std::wcout << L"序号 | 类型 | 文件名 | 大小(字节)\n";
std::wcout << L"---------------------------------------------------------\n";
for (int i = 0; i < fileList.size(); i++) {
const auto& file = fileList[i];
std::wcout << std::setw(3) << i + 1 << L" | ";
std::wcout << (file.isDirectory ? L"文件夹 " : L"文件 ") << L" | ";
std::wcout << std::setw(25) << file.name.substr(0, 25) << L" | "; // 限制文件名长度
std::wcout << file.fileSize << L"\n";
}
std::wcout << L"---------------------------------------------------------\n";
std::wcout << L"输入文件序号打开(输入0退出):";
}
// 4. 打开选中的文件/文件夹
void OpenFile(const std::wstring& filePath) {
// 调用系统默认程序打开文件/文件夹
ShellExecuteW(NULL, L"open", filePath.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
int main() {
// 设置控制台为宽字符模式(支持中文文件名)
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
// 获取桌面路径并遍历文件
std::wstring desktopPath = GetDesktopPath();
std::vector<FileInfo> fileList = TraverseDesktopFiles(desktopPath);
if (fileList.empty()) {
MessageBoxW(NULL, L"桌面无文件或文件夹!", L"提示", MB_ICONINFORMATION);
return 0;
}
// 主交互循环
int choice = 0;
while (true) {
ShowFileList(fileList);
std::wcin >> choice;
if (choice == 0) {
break; // 输入0退出
} else if (choice > 0 && choice <= fileList.size()) {
OpenFile(fileList[choice - 1].path); // 打开选中文件
MessageBoxW(NULL, L"已调用默认程序打开!", L"提示", MB_ICONINFORMATION);
} else {
MessageBoxW(NULL, L"输入序号无效!", L"错误", MB_ICONERROR);
}
}
return 0;
}
核心功能说明
1. 获取桌面路径:通过 SHGetFolderPathW 调用 Windows API 获取系统桌面目录(兼容旧系统,Win10/11 也可改用 SHGetKnownFolderPath )。
2. 遍历文件:用 FindFirstFileW / FindNextFileW 遍历桌面所有文件,筛选出有效文件/文件夹并记录名称、路径、类型、大小。
3. 控制台交互:以表格形式显示文件列表,支持用户输入序号打开文件(调用系统默认程序,如用记事本打开 txt、用资源管理器打开文件夹)。
4. 宽字符支持:通过 wstring 和 W 后缀 API(如 FindFirstFileW ),解决中文文件名乱码问题。
编译与运行注意事项
1. 环境要求:需在 Windows 系统下编译,依赖 shell32.lib 库(代码中已用 #pragma comment 自动链接)。
2. IDE 配置:用 Visual Studio 新建“控制台应用”项目,将代码替换默认文件,直接编译运行(无需额外依赖库)。
3. 功能扩展方向:若需更复杂功能(如文件复制/删除、图形界面),可进一步:
- 增加 CopyFileW / DeleteFileW 实现文件操作;
- 基于 MFC 或 Qt 开发图形界面(替代控制台交互);
- 增加文件搜索、排序(按名称/大小/修改时间)功能。

浙公网安备 33010602011771号