Delphi - 将物理路径(设备文件句柄)转换为虚拟路径

时间:2012-12-04 04:55:20

标签: delphi path virtual

如何转换

之类的路径

\设备\ HarddiskVolume3 \视窗

进入相应的虚拟路径? (比如c:\ Windows在这种情况下)

2 个答案:

答案 0 :(得分:6)

我个人更喜欢本土方式:

function GetHDDDevicesWithDOSPath:TStringlist;
var
  i: integer;
  root: string;
  device: string;
  buffer: string;
begin
  setlength(buffer, 1000);
  result:=TStringlist.create;
  for i := Ord('c') to Ord('z') do
  begin
    root := Char(i) + ':';
    if (QueryDosDevice(PChar(root), pchar(buffer), 1000) <> 0) then
    begin
      device := pchar(buffer);
      result.add(format('%s = %s\',[device, root ]));
    end;
  end;
end;

NB :此代码示例来自:http://www.delphipraxis.net/165249-auflistung-devices.html

这将返回逻辑驱动器和路径之间的映射。就我而言:

\Device\HarddiskVolume2 = c:\
\Device\HarddiskVolume3 = d:\
\Device\IsoCdRom0 = e:\
\Device\CdRom0 = f:\
\Device\hgfs\;Z:0000000000084af9\vmware-host\Shared Folders = z:\

您必须使用相应的驱动器号替换路径中的“\ device \ harddisk”部分。

请注意,驱动器号取决于用户。 一些有用的链接:

答案 1 :(得分:2)

其中一种方法是使用WMI,例如http://www.magsys.co.uk/delphi/magwmi.asp - 它还有一个可用于从中获取代码示例的演示。

您可以找到许多WMI Explorers并探索configuartiona类并构建查询。仅列举一些免费的:

  • Microsoft WMI CIM Studio在查询时很糟糕,但查看类很不错
  • 来自www.ks-soft.net的WMI Explorer变成了我最常用的

您还需要谷歌搜索WMI查询语言示例并阅读specifications:虽然WMI语言类似于SQL查询,但它有一些不同的语法和一组难以预测的非统一限制。


我个人在不同的方向上使用它:我需要制作一个按物理磁盘或网络服务器分组的卷(驱动器号)列表。

我结束了(快速而且相当丑陋,但足以在程序初始化中完成一次性工作)单位,如下所示。你可以对它进行流式处理,你肯定必须反转请求和功能。

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.

我没有尝试过无字母卷(例如c:\和c:\ Windows将是一个分区,c:\ Windows \ Temp将驻留在另一个磁盘上),但是如果你需要考虑这样的话配置,我相信您可以在WMI资源管理器中进行正确的查询,并将其添加到您的程序中。

相关问题