gpt4 book ai didi

c++ - 如何获取 MFC 中可用(未使用)的盘符?

转载 作者:行者123 更新时间:2023-11-30 04:37:28 26 4
gpt4 key购买 nike

如何使用 C++ 获取 MFC 中可用(未使用)的盘符?任何代码片段..

最佳答案

来自 Here :

这会为您提供正在使用的驱动器,只需将它们从字母表 A-Z 的其余部分中取出即可

 ////////////////////////////////////////////////////////////////
// MSDN Magazine -- April 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0. Set tabsize = 3 in your editor.
// Runs in Windows XP and probably Windows 2000 too.
//
#include "stdafx.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std; // for string class

//////////////////
// This mini-table maps GetDriveType codes to human-readable string
//
struct {
UINT type; // return code from GetDriveType
LPCSTR name; // ascii name
} DriveTypeFlags [] = {
{ DRIVE_UNKNOWN, "Unknown" },
{ DRIVE_NO_ROOT_DIR, "Invalid path" },
{ DRIVE_REMOVABLE, "Removable" },
{ DRIVE_FIXED, "Fixed" },
{ DRIVE_REMOTE, "Network drive" },
{ DRIVE_CDROM, "CD-ROM" },
{ DRIVE_RAMDISK, "RAM disk" },
{ 0, NULL},
};

//////////////////
// Standard tmain for MFC ListDrives app
//
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
cerr << _T("Fatal Error: MFC initialization failed") << endl;
return -1;
}

// Get logical drive strings-- a:\b:\c:\... etc.
// Could also use GetLogicalDrives to get in the form of a bitmap instead
// of character string.
//
TCHAR buf[100];
DWORD len = GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf);

// Display information about each drive.
//
string msg = "Logical Drives:\n"; // STL string
for (TCHAR* s=buf; *s; s+=_tcslen(s)+1) {
LPCTSTR sDrivePath = s;
msg += sDrivePath;
msg += " ";

// GetDriveType gets one of the enum values DRIVE_UNKNOWN, etc.
//
UINT uDriveType = GetDriveType(sDrivePath);

// Find drive type in table. I do a table lookup here to be extra
// cautious, but since the uDriveType values are sequential, i could've
// used DriveTypeFlags[uDriveType] instead of linear lookup. In
// practice you would usually perform some check like
//
// if (uDriveType & DEVICE_CDROM) {
// ...
// }
//
for (int i=0; DriveTypeFlags[i].name; i++) {
if (uDriveType == DriveTypeFlags[i].type) {
msg += DriveTypeFlags[i].name;
break;
}
}
msg += '\n';
}

cout << msg.c_str();

return 0;
}

关于c++ - 如何获取 MFC 中可用(未使用)的盘符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com