系统注册表存储操作

1.定义Key常量:  下面三个常量可所以定义, 本人是为了程序注册写得一个简单的这侧表存取功能

  const
   _RootKey =  HKEY_LOCAL_MACHINE;
   _CurrentKey = '\SoftWare\HANDY';
   _SoftKeyItem = 'serial_number';
   _SoftCodeID = 'soft_codeid';
   _HDID = 'hd_id';

2.具体函数实现

 /// <summary>
 /// 读取 注册表字符串
 /// </summary>

class function TResCommand.FindRegKeyString(Param_RootKey:HKey;Param_CurrentKey,Param_KeyItem:String):string;
var
  MyRegistry:TRegistry;
begin
  MyRegistry:=TRegistry.Create;
  with MyRegistry do
  begin
    RootKey:=Param_RootKey;
    if OpenKey(Param_CurrentKey,False)=True then
    begin
      try
        Result:=ReadString(Param_KeyItem);
      except
        Result:='';
      end;
      CloseKey();
    end
    else
      Result:='';
    Free;
  end;
end;

  /// <summary>

 ///  读取 注册表二进制
 /// </summary>
class function TResCommand.FindRegKeyInteger(Param_RootKey:HKey;Param_CurrentKey,Param_KeyItem:String):Integer;
var
  MyRegistry:TRegistry;
begin
  MyRegistry:=TRegistry.Create;
  with MyRegistry do
  begin
    RootKey:=Param_RootKey;
    if OpenKey(Param_CurrentKey,False)=True then
    begin
      try
        Result:=ReadInteger(Param_KeyItem);
      except
        Result:=0;
      end;
      CloseKey();
    end
    else
    Result:=0;
    Free;
  end;
end;

//写入

class procedure TResCommand.WriteRegKeyInteger(Param_RootKey:HKey;Param_CurrentKey,Param_KeyItem:String;Param_KeyInteger:Integer);
var
  MyRegistry:TRegistry;
begin
  MyRegistry:=TRegistry.Create;
  with MyRegistry do
  begin
    RootKey:=Param_RootKey;
    if OpenKey(Param_CurrentKey,True)=True then
    begin
      try
        WriteInteger(Param_KeyItem,Param_KeyInteger);
      except
        on ex:Exception do
        msgbox(ex.Message,MB_ICONSTOP);
      end;
      CloseKey();
    end
    else
    msgbox('没有找到键值!',MB_ICONSTOP);
    Free;
  end;
end;

class procedure TResCommand.WriteRegKeyString(Param_RootKey:HKey;Param_CurrentKey,Param_KeyItem,Param_KeyString:String);
var
  MyRegistry:TRegistry;
begin
  MyRegistry:=TRegistry.Create;
  with MyRegistry do
  begin
    RootKey:=Param_RootKey;
    if OpenKey(Param_CurrentKey,True)=True then
    begin
      try
        WriteString(Param_KeyItem,Param_KeyString);
      except
        on ex:Exception do
        begin
          msgbox(ex.Message,MB_ICONSTOP);
        end;
      end;
      CloseKey();
    end
    else
      msgbox('没有找到键值!',MB_ICONSTOP);
    Free;
  end;
end;

//删除


class procedure TResCommand.DeleteRegKeyWord(Param_RootKey:HKey;Param_CurrentKey,Param_Value:String);
var
  MyRegistry:TRegistry;
begin
  MyRegistry:=TRegistry.Create;
  with MyRegistry do
  begin
    RootKey:=Param_RootKey;
    if OpenKey(Param_CurrentKey,True)=True then
    begin
      try
        DeleteValue(Param_Value); //删除注册表值
        Deletekey(Param_CurrentKey); //删除项
        CloseKey;
        Free;
      except
        on ex:Exception do
          msgbox(ex.Message,MB_ICONSTOP);
      end;
    end
    else
      msgbox('没有找到键值!',MB_ICONSTOP);
    Free;
  end;
end;

posted @ 2011-07-30 10:16  神码都在云端  阅读(393)  评论(0编辑  收藏  举报