Delphi 检测U盘状态

uses StdCtrls,shlobj,Activex,ComObj;

 

procedure WMDEVICECHANGE(var msgx:Tmessage);message WM_DEVICECHANGE;

 

function GetLastMoveDrive:String;
var
Drivetype:Integer;
root:String;
i:Integer;
begin
Result:='';
for i:=67 to 91 do //从C:盘开始到Z:盘
begin
root:=char(i)+':\';
Drivetype:=GetDriveType(pChar(root));
if Drivetype=DRIVE_REMOVABLE then
begin
Result:='插入U盘 '+root;
Break;
end;
end;
end;

procedure WMDEVICECHANGE(var msgx: Tmessage);
const
DBT_DEVICEARRIVAL=$8000;
DBT_DEVICEREMOVECOMPLETE=$8004;
begin
inherited;
case msgx.WParam of
DBT_DEVICEARRIVAL:
Label1.Caption:=GetLastMoveDrive;
DBT_DEVICEREMOVECOMPLETE:
Label1.Caption:='U盘被取走';
end;
end;

 

 

 

 

procedure TForm1.Button1Click(Sender: TObject);
var
reg:TRegistry;
begin
Try
reg:=tregistry.create;
reg.rootkey:=HKEY_LOCAL_MACHINE;
reg.openkey('\SYSTEM\CurrentControlSet\Control',true);

if reg.CreateKey('\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies') then
begin
reg.openkey('\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies',true);
reg.WriteInteger('WriteProtect',1); //添加注册表项和建值
end;

reg.CloseKey; //关闭KEY
reg.Free; //释放
MessageBox(Handle,'禁止U盘拷入功能','提示',MB_OK);
Except
MessageBox(Handle,'设置失败','提示',MB_OK);
end;

end;

 

procedure TForm1.Button2Click(Sender: TObject);
var
reg:tregistry;
begin
Try
reg:=tregistry.Create;
reg.rootkey:=HKEY_LOCAL_MACHINE;
reg.DeleteKey('\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies'); //删除注册表项
reg.CloseKey;
reg.Free;
MessageBox(Handle,'解除禁止U盘拷入功能','提示',MB_OK);
Except
MessageBox(Handle,'设置失败','提示',MB_OK);
end;
end;

转载于:https://www.cnblogs.com/lzj1981/archive/2013/04/28/3049394.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值