Delphi编程隐藏硬盘分区

 都知道修改注册表可以在我的电脑中隐藏硬盘分区的吧!找到HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
后,看到的NoDrives是二进制值,也可以是十进制值。而且一个驱动器的值是前面驱动器的值的两倍:A = 1 , B = 2 , C = 4 , D = 8 , E = 16 ......依次类推。
例如:如果想隐藏盘符C和盘符E,就要将 4 和 16 相加,结果是 4 + 16 = 20 ,然后调用 HideDriver(20);

uses Registry;

procedure HideDriver(HideNumber: integer);
var Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    with Reg do
    begin
      RootKey := HKEY_CURRENT_USER;
      OpenKey(Software\Microsoft\Windows\CurrentVersion\Policies\Explorer, True);
      WriteInteger(NoDrives, HideNumber);
      CloseKey;

      RootKey := HKEY_LOCAL_MACHINE;
      OpenKey(Software\Microsoft\Windows\CurrentVersion\Policies\Explorer, True);
      WriteInteger(NoDrives, HideNumber);
      CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

增加下列代码在一个 Button 的 OnClick 事件中,可以让我的电脑里隐藏的驱动器重新显示,但必须重新启动计算机。

var Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    with Reg do
    begin
      RootKey := HKEY_CURRENT_USER;
      OpenKey(Software\Microsoft\Windows\CurrentVersion\Policies\Explorer, True);
      DeleteValue(NoDrives);
      CloseKey;

      RootKey := HKEY_LOCAL_MACHINE;
      OpenKey(Software\Microsoft\Windows\CurrentVersion\Policies\Explorer, True);
      DeleteValue(NoDrives);
      CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;
[本文来自: 学Delphi网(http://www.xuedelphi.com/) ]详细出处参考:http://www.xuedelphi.cn/article/html2010/2007122718043189.html

你可能感兴趣的:(Delphi)