gpt4 book ai didi

windows - 如何使用 Delphi 在 Windows 中从 USB 驱动器挂载分区?

转载 作者:可可西里 更新时间:2023-11-01 10:40:05 28 4
gpt4 key购买 nike

我想在 Windows (XP) 中挂载 USB 驱动器的所有分区。我的意思是我想为他们每个人分配驱动器号(当他们没有时)。操作系统会自动执行此操作,但在某些情况下此类程序很有用。

我知道如何查找驱动器是否在 USB 上。到目前为止我的代码是:

type
STORAGE_QUERY_TYPE = (PropertyStandardQuery = 0, PropertyExistsQuery, PropertyMaskQuery, PropertyQueryMaxDefined);
TStorageQueryType = STORAGE_QUERY_TYPE;

STORAGE_PROPERTY_ID = (StorageDeviceProperty = 0, StorageAdapterProperty);
TStoragePropertyID = STORAGE_PROPERTY_ID;

STORAGE_PROPERTY_QUERY = packed record
PropertyId: STORAGE_PROPERTY_ID;
QueryType: STORAGE_QUERY_TYPE;
AdditionalParameters: array[0..9] of AnsiChar;
end;
TStoragePropertyQuery = STORAGE_PROPERTY_QUERY;

STORAGE_BUS_TYPE = (BusTypeUnknown = 0, BusTypeScsi, BusTypeAtapi, BusTypeAta, BusType1394, BusTypeSsa, BusTypeFibre,
BusTypeUsb, BusTypeRAID, BusTypeiScsi, BusTypeSas, BusTypeSata, BusTypeMaxReserved = $7F);
TStorageBusType = STORAGE_BUS_TYPE;

STORAGE_DEVICE_DESCRIPTOR = packed record
Version: DWORD;
Size: DWORD;
DeviceType: Byte;
DeviceTypeModifier: Byte;
RemovableMedia: Boolean;
CommandQueueing: Boolean;
VendorIdOffset: DWORD;
ProductIdOffset: DWORD;
ProductRevisionOffset: DWORD;
SerialNumberOffset: DWORD;
BusType: STORAGE_BUS_TYPE;
RawPropertiesLength: DWORD;
RawDeviceProperties: array[0..0] of AnsiChar;
end;
TStorageDeviceDescriptor = STORAGE_DEVICE_DESCRIPTOR;

const
IOCTL_STORAGE_QUERY_PROPERTY = $002D1400;

var i: Integer;
H: THandle;
USBDrives: array of Byte;
Query: TStoragePropertyQuery;
dwBytesReturned: DWORD;
Buffer: array[0..1023] of Byte;
sdd: TStorageDeviceDescriptor absolute Buffer;

begin
SetLength(UsbDrives, 0);
SetErrorMode(SEM_FAILCRITICALERRORS);
for i := 0 to 99 do
begin
H := CreateFile(PChar('\\.\PhysicalDrive' + IntToStr(i)), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if H <> INVALID_HANDLE_VALUE then
begin
try
dwBytesReturned := 0;
FillChar(Query, SizeOf(Query), 0);
FillChar(Buffer, SizeOf(Buffer), 0);
sdd.Size := SizeOf(Buffer);
Query.PropertyId := StorageDeviceProperty;
Query.QueryType := PropertyStandardQuery;
if DeviceIoControl(H, IOCTL_STORAGE_QUERY_PROPERTY, @Query, SizeOf(Query), @Buffer, SizeOf(Buffer), dwBytesReturned, nil) then
if sdd.BusType = BusTypeUsb then
begin
SetLength(USBDrives, Length(USBDrives) + 1);
UsbDrives[High(USBDrives)] := Byte(i);
end;
finally
CloseHandle(H);
end;
end;
end;
for i := 0 to High(USBDrives) do
begin
//
end;
end.

但我不知道如何访问每个驱动器上的分区并挂载它们。你能帮帮我吗?

最佳答案

查看卷管理功能 - http://msdn.microsoft.com/en-us/library/aa365730%28v=vs.85%29.aspx

FirstFirstVolume、FindNextVolume、FindVolumeClose 会将分区列为\\?\Volume{Guid} 名称。

Createfile 应该能够选择卷名称,以便您上面的代码可以检查它是 USB。

SetVolumeMountPoint 可让您将卷挂载为驱动器号或文件夹挂载点。

此博客条目讨论卷名 - http://blogs.msdn.com/b/adioltean/archive/2005/04/16/408947.aspx

关于windows - 如何使用 Delphi 在 Windows 中从 USB 驱动器挂载分区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645162/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com