C++ 枚举 Windows 中所有可用的驱动器号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/286534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enumerating all available drive letters in Windows
提问by sharkin
I want to enumerate all available drive letters (which aren't already taken) in Windows using VC++.
我想使用 VC++ 枚举 Windows 中所有可用的驱动器号(尚未使用)。
How can I do this?
我怎样才能做到这一点?
采纳答案by JTeagle
::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 的术语中)调用::GetDriveType(),并且应该返回或者驱动器是否可用。":\"
":\\"
_T(":\\")
DRIVE_UNKNOWN
DRIVE_NO_ROOT_DIR
回答by Alnitak
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
可用于获取有关特定驱动器的更多信息。
回答by SturmCoder
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++ 或代码块上对它进行了编码,以检查使用 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++;
}
}
}
回答by VonC
The GetLogicalDriveStrings Functionis a good starting point.
该GetLogicalDriveStrings功能是一个很好的起点。
回答by bogdan
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
回答by user_number153
std::vector<std::string> getListOfDrives() {
std::vector<std::string> arrayOfDrives;
char* szDrives = new char[MAX_PATH]();
if (GetLogicalDriveStringsA(MAX_PATH, szDrives));
for (int i = 0; i < 100; i += 4)
if (szDrives[i] != (char)0)
arrayOfDrives.push_back(std::string{szDrives[i],szDrives[i+1],szDrives[i+2]});
delete[] szDrives;
return arrayOfDrives;
}
returns a vector of drives e.g. C:\D:\E:\F:\
返回驱动器向量,例如 C:\D:\E:\F:\
std::vector<std::string> drives = getListOfDrives();
for (std::string currentDrive : drives) {
std::cout << currentDrive << std::endl;
}
enumerateing over them
列举他们