枚举Windows中所有可用的驱动器号

时间:2023-01-26 21:24:58

I want to enumerate all available drive letters (which aren't already taken) in Windows using VC++.

我想使用VC ++枚举Windows中所有可用的驱动器号(尚未使用)。

How can I do this?

我怎样才能做到这一点?

5 个解决方案

#1


13  

::GetLogicalDrives() returns a list of available (read: used) drives as bits in a mask. This should include mapped network drives. Thus, you can simply walk the bits to find bits that are zero, meaning no drive is present. If in doubt, you can always call ::GetDriveType() with the drive letter + ":\" (":\\" in C code, or _T(":\\") in Unicode-aware terminology, of course), and that should return DRIVE_UNKNOWN or DRIVE_NO_ROOT_DIR if the drive is available.

:: GetLogicalDrives()返回一个可用(读取:已使用)驱动器列表作为掩码中的位。这应包括映射的网络驱动器。因此,您可以简单地遍历位以查找零位,这意味着不存在驱动器。如果有疑问,你总是可以用驱动器号+“:\”(C代码中的“::\\”或者用Unicode识别的术语中的_T(“:\\”)调用:: GetDriveType()) ,如果驱动器可用,则应返回DRIVE_UNKNOWN或DRIVE_NO_ROOT_DIR。

#2


6  

GetLogicalDriveStrings can get you just the list of currently used drive letters.

GetLogicalDriveStrings可以为您提供当前使用的驱动器号列表。

GetVolumeInformation can be used to get more information about a specific drive.

GetVolumeInformation可用于获取有关特定驱动器的更多信息。

#3


3  

The GetLogicalDriveStrings Function is a good starting point.

GetLogicalDriveStrings函数是一个很好的起点。

#4


3  

Im not shure how to enumerate them or if it will compile on visual c++ but I sturm-coded this on Dev C++ or Code Blocks to check what drive is acessible by using CreateFile and what type of drive is by using GetDriveType. Program checks drives from A to Z:

我不知道如何枚举它们或者它是否会在visual c ++上编译,但我在Dev C ++或Code Blocks上对此进行了sturm编码,以通过使用CreateFile以及使用GetDriveType来驱动什么类型的驱动器来检查哪些驱动器是可访问的。程序检查驱动器从A到Z:

#include <windows.h>
#include <cstring>
#include <sstream>
#include <iostream>

using namespace std;

int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nShowCmd)
{
    HANDLE hDevice = NULL;
    HANDLE fileFind = NULL;
    while(true)
    {
        Sleep(3005);
        char drv='A';
        while(drv!='[')
        {
            Sleep(105);
            const char *charDrvCF;
            const char *charDrv;
            stringstream Str;
            string drvStr;
            Str<<drv;
            Str>>drvStr;
            string drvSpc=drvStr+":\\";
            string fCheck="\\\\.\\";
            string fhCheck=fCheck+drvStr+":";
            charDrvCF=fhCheck.c_str();
            charDrv=drvSpc.c_str();      
            hDevice=CreateFile(charDrvCF,
                                GENERIC_READ|GENERIC_WRITE,
                                FILE_SHARE_READ|FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL);
            if(hDevice!=INVALID_HANDLE_VALUE)
            {
                switch(GetDriveType(charDrv))
                {
                    case DRIVE_FIXED:
                    {
                        cout<<"Fixed drive detected: "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_REMOVABLE:
                    {
                        cout<<"Removable drive detected: "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_NO_ROOT_DIR:
                    {
                        cout<<"There is no volume mounted at the specified path. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_REMOTE:
                    {
                        cout<<"The drive is a remote (network) drive. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_CDROM:
                    {
                        cout<<"The drive is a CD-ROM drive. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_RAMDISK:
                    {
                        cout<<"The drive is a RAM disk. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_UNKNOWN:
                    {
                        cout<<"The drive type cannot be determined. "<<charDrv<<endl;
                        break;
                    }
                }
            }
        drv++;
        }
    }
}

#5


1  

GetLogicalDrives and GetLogicalDriveStrings are not seeing network drives created in a different namespace.

GetLogicalDrives和GetLogicalDriveStrings没有看到在不同的命名空间中创建的网络驱动器。

For example calling the functions from a service running under Local System will not see the network drives created by a logged user.

例如,从在本地系统下运行的服务调用函数将不会看到已记录用户创建的网络驱动器。

This is happening starting with Windows XP. The following article describes this case: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363908(v=vs.85).aspx

这是从Windows XP开始的。以下文章描述了这种情况:http://msdn.microsoft.com/en-us/library/windows/desktop/aa363908(v = vs。85).aspx

#1


13  

::GetLogicalDrives() returns a list of available (read: used) drives as bits in a mask. This should include mapped network drives. Thus, you can simply walk the bits to find bits that are zero, meaning no drive is present. If in doubt, you can always call ::GetDriveType() with the drive letter + ":\" (":\\" in C code, or _T(":\\") in Unicode-aware terminology, of course), and that should return DRIVE_UNKNOWN or DRIVE_NO_ROOT_DIR if the drive is available.

:: GetLogicalDrives()返回一个可用(读取:已使用)驱动器列表作为掩码中的位。这应包括映射的网络驱动器。因此,您可以简单地遍历位以查找零位,这意味着不存在驱动器。如果有疑问,你总是可以用驱动器号+“:\”(C代码中的“::\\”或者用Unicode识别的术语中的_T(“:\\”)调用:: GetDriveType()) ,如果驱动器可用,则应返回DRIVE_UNKNOWN或DRIVE_NO_ROOT_DIR。

#2


6  

GetLogicalDriveStrings can get you just the list of currently used drive letters.

GetLogicalDriveStrings可以为您提供当前使用的驱动器号列表。

GetVolumeInformation can be used to get more information about a specific drive.

GetVolumeInformation可用于获取有关特定驱动器的更多信息。

#3


3  

The GetLogicalDriveStrings Function is a good starting point.

GetLogicalDriveStrings函数是一个很好的起点。

#4


3  

Im not shure how to enumerate them or if it will compile on visual c++ but I sturm-coded this on Dev C++ or Code Blocks to check what drive is acessible by using CreateFile and what type of drive is by using GetDriveType. Program checks drives from A to Z:

我不知道如何枚举它们或者它是否会在visual c ++上编译,但我在Dev C ++或Code Blocks上对此进行了sturm编码,以通过使用CreateFile以及使用GetDriveType来驱动什么类型的驱动器来检查哪些驱动器是可访问的。程序检查驱动器从A到Z:

#include <windows.h>
#include <cstring>
#include <sstream>
#include <iostream>

using namespace std;

int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nShowCmd)
{
    HANDLE hDevice = NULL;
    HANDLE fileFind = NULL;
    while(true)
    {
        Sleep(3005);
        char drv='A';
        while(drv!='[')
        {
            Sleep(105);
            const char *charDrvCF;
            const char *charDrv;
            stringstream Str;
            string drvStr;
            Str<<drv;
            Str>>drvStr;
            string drvSpc=drvStr+":\\";
            string fCheck="\\\\.\\";
            string fhCheck=fCheck+drvStr+":";
            charDrvCF=fhCheck.c_str();
            charDrv=drvSpc.c_str();      
            hDevice=CreateFile(charDrvCF,
                                GENERIC_READ|GENERIC_WRITE,
                                FILE_SHARE_READ|FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL);
            if(hDevice!=INVALID_HANDLE_VALUE)
            {
                switch(GetDriveType(charDrv))
                {
                    case DRIVE_FIXED:
                    {
                        cout<<"Fixed drive detected: "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_REMOVABLE:
                    {
                        cout<<"Removable drive detected: "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_NO_ROOT_DIR:
                    {
                        cout<<"There is no volume mounted at the specified path. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_REMOTE:
                    {
                        cout<<"The drive is a remote (network) drive. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_CDROM:
                    {
                        cout<<"The drive is a CD-ROM drive. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_RAMDISK:
                    {
                        cout<<"The drive is a RAM disk. "<<charDrv<<endl;
                        break;
                    }
                    case DRIVE_UNKNOWN:
                    {
                        cout<<"The drive type cannot be determined. "<<charDrv<<endl;
                        break;
                    }
                }
            }
        drv++;
        }
    }
}

#5


1  

GetLogicalDrives and GetLogicalDriveStrings are not seeing network drives created in a different namespace.

GetLogicalDrives和GetLogicalDriveStrings没有看到在不同的命名空间中创建的网络驱动器。

For example calling the functions from a service running under Local System will not see the network drives created by a logged user.

例如,从在本地系统下运行的服务调用函数将不会看到已记录用户创建的网络驱动器。

This is happening starting with Windows XP. The following article describes this case: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363908(v=vs.85).aspx

这是从Windows XP开始的。以下文章描述了这种情况:http://msdn.microsoft.com/en-us/library/windows/desktop/aa363908(v = vs。85).aspx