如何在MFC中获取可用(未使用)的驱动器号?

时间:2010-09-28 08:44:31

标签: c++ mfc

如何使用C ++获取MFC中可用(未使用)的驱动器号? 任何代码段..

4 个答案:

答案 0 :(得分:2)

来自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;
}

答案 1 :(得分:0)

您可能在GetLogicalDrives之后,这会让您对系统使用的所有驱动器号有点掩盖,您可以将它们转换为字母并将它们添加到组合框中


使其更清晰:

  

返回值

     

如果功能成功,   返回值是位掩码   代表当前可用的   磁盘驱动器。位位置0(   最低有效位是驱动器A,位   位置1是驱动器B,位位置2   是驱动器C,依此类推。

所以GetLogicalDrives() & 1检查驱动器A是否存在,GetLogicalDrives() & 4检查驱动器C是否存在

答案 2 :(得分:0)

致电GetLogicalDrives()。设置为零的位对应于未使用的驱动器号(当然位26-31除外)

答案 3 :(得分:0)

使用GetLogicalDrives()并获取驱动器号的最简单方法是使用字符串列表。将字符串的索引链接到表示GetLogicalDrives()返回的位掩码中的驱动器的位的位置。这是一个使用QStringList

的例子
DWORD bitmask = GetLogicalDrives();
QStringList driveList = QStringList();
//Can add the rest of the alphabet.
driveList << "a:" << "b:" << "c:" << "d:" << "e:" << "f:" << "g:" << "h:" << "i:"; 
for(int i = 0; i < driveList.size(); i++)
{
    if((bitmask & (1 << i)) == 0) //Shift bitmask and if 0 drive is free
    {
        driveList.at(i) // String of the free drive.
    }
}