使用MagWMi包装器及其一行函数(一行很慢,但这不是需要速度的地方)。
我用它来制作二级磁盘列表。
-
硬盘驱动器1->字母1、字母2
-
硬盘驱动器2->字母3、字母4
-
网络服务器1->字母5、字母6
-
网络服务器2->字母7、字母8
-
无法识别->所有其他字母
但你必须记住,有些磁盘可能没有任何字母。
所以这会告诉你有关根目录的信息,但不会更多。
磁盘C:可能派生为多个物理磁盘。
有些物理磁盘可能根本没有字母。
unit WMI_Helper;
interface
function WMINetDiskName(const disk: string { 'C:' - w/o slash } ): string;
function WMIPhysDiskName(const disk: string { 'C:' - w/o slash } ): string;
function WMIGetVolumeName(const disk: string { 'C:' - w/o slash } ): string;
implementation
uses magwmi, SysUtils, StrUtils, Windows, IOUtils;
function WMIGetProp(const query, param, resultProp: string): string;
begin
if MagWmiGetOneQ(StringReplace(query, '%s', param, []), resultProp, Result) <= 0
then
Result := '';
Result := Trim(Result);
end;
function WMINetDiskName(const disk: string { 'C:' - w/o slash } ): string;
const
req = 'select ProviderName from Win32_MappedLogicalDisk where DeviceID = "%s"';
prop = 'ProviderName';
var
i: integer;
begin
Result := WMIGetProp(req, disk, prop);
If not TPath.IsUNCPath(Result) then
exit('');
i := PosEx('\', TPath.GetPathRoot(Result), 3);
if i <= 0 then
exit('');
SetLength(Result, i - 1);
end;
function WMIPhysDiskName(const disk: string { 'C:' - w/o slash } ): string;
const
resultProp = 'DeviceID';
reqPart = 'ASSOCIATORS OF {Win32_LogicalDisk.DeviceID="%s"} WHERE ResultClass=Win32_DiskPartition';
reqDisk = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID="%s"} WHERE ResultClass=Win32_DiskDrive';
begin
Result := WMIGetProp(reqPart, disk, resultProp);
if Result > '' then
Result := WMIGetProp(reqDisk, Result, resultProp);
end;
function WMIGetVolumeName(const disk: string { 'C:' - w/o slash } ): string;
const
prop = 'VolumeName';
reqNet = 'select VolumeName from Win32_MappedLogicalDisk where DeviceID = "%s"';
reqPhy = 'select VolumeName from Win32_LogicalDisk where DeviceID = "%s"';
begin
Result := WMIGetProp(IfThen(GetDriveType(PChar(disk)) = DRIVE_REMOTE, reqNet,
reqPhy), disk, prop);
end;
end.