首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

超难有关问题! 怎么 根据 盘符 判断 是不是 U盘 ? 急

2012-03-16 
超难问题!!!!! 如何 根据 盘符 判断 是不是 U盘 ??????????????? 急!!!!!!!!!!!!!!!!!!注意不能用GetDrive

超难问题!!!!! 如何 根据 盘符 判断 是不是 U盘 ??????????????? 急!!!!!!!!!!!!!!!!!!
注意不能用   GetDriveType   判断   ,   因为   GetDriveType   对某些U盘   判断为   固定的磁盘!!!!!!!!!


请给出详细   的代码   谢谢!!!!!!!!!!!!!!!!!!!!!


[解决办法]
用WMI肯定可以(WMI可得到系统所有信息),以下为例程(当然你可以通过Win32_USBControllerDevice或其它项目来判):


unit wmi;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses
ActiveX, WbemScripting_TLB;
function ADsEnumerateNext(pEnumVariant: IEnumVARIANT; cElements: ULONG;
var pvar: OleVARIANT; var pcElementsFetched: ULONG): HRESULT; safecall; external 'activeds.dll ';

procedure DumpWMI_Process(Process: SWBemObject);
var
Enum: IEnumVARIANT;
varArr: OleVariant;
lNumElements: ULong;
SProp: ISWbemProperty;
Prop: OleVariant;
PropName: string;
PropType: string;
PropValue: string;
begin
Form1.Memo1.Lines.Add( '+ WMI Path: ' + Process.Path_.Path);
Enum := Process.Properties_._NewEnum as IEnumVariant;
while (Succeeded(ADsEnumerateNext(Enum, 1, VarArr, lNumElements))) and
(lNumElements > 0) do
begin
if Succeeded(IDispatch(varArr).QueryInterface(SWBemProperty, SProp)) and
Assigned(SProp) then
begin
try
PropName := SProp.Name;
Prop := SProp.Get_Value;
PropType := inttostr((VarType(Prop)));
PropValue := VarToStr(Prop);
Form1.Memo1.Lines.Add( ' + ' + PropName + '[ ' + PropType + '] = ' + PropValue);
except
on E: Exception do
begin
// WriteLn(ErrOutput, PropName, ': ', E.Message);
end;
end;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
Server: string;
Enum: IEnumVARIANT;
varArr: OleVariant;
lNumElements: ULong;
AName: array[0..255] of Char;
ASize: DWORD;
begin
if (ParamCount = 0) then
begin
Server := ' ';
ASize := SizeOf(AName) - 1;
if GetComputerName(@AName, ASize) then Server := AName;
end
else
begin
Server := ParamStr(1);
end;
try
Memo1.Lines.BeginUpdate;
Enum := CoSWbemLocator.Create.ConnectServer(Server, 'root\cimv2 ', ' ',
' ', ' ', ' ', 0, nil).ExecQuery( 'Select InterfaceType from Win32_DiskDrive ', 'WQL ',
wbemFlagBidirectional, nil)._NewEnum as IEnumVariant;
while (Succeeded(ADsEnumerateNext(Enum, 1, varArr, lNumElements))) and
(lNumElements > 0) do
begin
DumpWMI_Process(IUnknown(varArr) as SWBemObject);
end;
finally
Memo1.Lines.EndUpdate;
end;
end;
////其中,WbemScripting_TLB.pas可从
///http://www.truth4all.org/WbemScripting_TLB.pas
///下载

end.
[解决办法]
InterfaceType[8] = USB


------解决方案--------------------


disksize:=0;
for i:=99 to 122 do // 'c '- 'z ';
begin
drive:=pchar(chr(i)+ ':\ ');
x:=GetDriveType(drive);
if x=3 then //3:代表介质是硬盘.
begin
GetDiskFreeSpaceEx(drive,userFreeBytes,totalBytes,@freeBytes);
k:=1000000000;
disksize:=totalBytes div k;
ListItem:=ListView1.Items.Add;
ListItem.Caption:=UpperCase(chr(i))+ ' 驱动器 ';
ListItem.SubItems.Add(Formatfloat( '###,##0 ',disksize)+ 'G ');
end;
if x=2 then //removable
begin
GetDiskFreeSpaceEx(drive,userFreeBytes,totalBytes,@freeBytes);
disksize:=totalBytes div 1048576;

ListItem:=ListView1.Items.add;
ListItem.Caption:=UpperCase(chr(i))+ ' 可移动磁盘 ';
ListItem.SubItems.Add(inttostr(disksize)+ 'M ');
end;
end;

热点排行