如何在Windows中列出物理磁盘?
为了获取可用的
"\\\\.\PhysicalDrive0"
列表。queryAndPrintResult(L"SELECT * FROM Win32_DiskDrive", L"Name");
今天在我的RSS阅读器中看到这个。我为你有更简洁的解决方案。这个示例是用Delphi编写的,但很容易转换为C/C++(都是Win32)。
从以下注册表位置查询所有值名称: HKLM\SYSTEM\MountedDevices
逐一将它们传递到以下函数中,您将返回设备名称。非常干净简单!我在这里找到了这段代码的博客。
function VolumeNameToDeviceName(const VolName: String): String;
var
s: String;
TargetPath: Array[0..MAX_PATH] of WideChar;
bSucceeded: Boolean;
begin
Result := ”;
// VolumeName has a format like this: \\?\Volume{c4ee0265-bada-11dd-9cd5-806e6f6e6963}\
// We need to strip this to Volume{c4ee0265-bada-11dd-9cd5-806e6f6e6963}
s := Copy(VolName, 5, Length(VolName) - 5);
bSucceeded := QueryDosDeviceW(PWideChar(WideString(s)), TargetPath, MAX_PATH) <> 0;
if bSucceeded then
begin
Result := TargetPath;
end
else begin
// raise exception
end;
end;
在Windows Powershell中
Get-WmiObject -Class Win32_DiskDrive -ComputerName <COMPUTERNAME> | select Name,SerialNumber,SCSITargetID,Size
列出美式英语字母表中除了a和b以外的所有字母。"CDEFGHIJKLMNOPQRSTUVWXYZ"。使用CreateFile
打开每个驱动器,例如CreateFile("\\.\C:")
。如果它没有返回INVALID_HANDLE_VALUE
,那么你就得到了一个“好”的驱动器。接下来,将该句柄传递给DeviceIoControl
以获取磁盘号码。有关更多详细信息,请参见我的相关答案。