Delphi 操作注册表文件(5)

来源:互联网 发布:php多维数组写法 编辑:程序博客网 时间:2024/05/22 03:18

uses Registry;

    Registry为我们提供了操作注册表的方法,这里我们用TRegIniFile提供的方法,TRegIniFile继承自TRegistry,TRegIniFile提供的方法类似于前面讲到的操作Ini文件的方法,这样就大大简化了我们操作"神秘"注册表的方法,下面是常用的方法:

 

  1. function CreateKey(const Key: String): Boolean;  创建Key
  2. function OpenKey(const Key: String; CanCreate: Boolean): Boolean; 打开Key,当CanCreate为True时,如果Key不存在,会自动创建
  3. procedure EraseSection(const Section: String); 删除
  4. WriteString,WriteInteger,WriteBool,WriteCurrency 写值
  5. ReadString,ReadInteger,ReadBool,ReadCurrency  读值
  6. ReadSection,ReadSections,ReadSectionValues 这些是不是很眼熟,和INI文件的方法一样

看例子

  • 创建节点

[c-sharp] view plaincopyprint?
  1. var  
  2.   reg:TRegIniFile;  
  3. begin  
  4.   reg := TRegIniFile.Create;               //创建实例  
  5.   reg.RootKey := HKey_Local_Machine;       //设置根值  
  6.   if reg.OpenKey('SOFTWARE/MyReg',True) then      //打开 HKey_Local_Machine/SOFTWARE/MyReg,如果MyReg不存在,则自动创建  
  7.   begin  
  8.     //在HKey_Local_Machine/SOFTWARE/MyReg下创建MySec项,然后在MySec中创建一个字符串MyValue,值为China  
  9.     reg.WriteString('MySec','MyValue','China');  
  10.   end;  
  11. end;  

  • 删除值

[delphi] view plaincopyprint?
  1. var  
  2.   reg:TRegIniFile;  
  3. begin  
  4.   reg := TRegIniFile.Create;               //创建实例  
  5.   reg.RootKey := HKey_Local_Machine;       //设置根值  
  6.   if reg.OpenKey('SOFTWARE/MyReg/MySec',True) then  
  7.   begin  
  8.     //删除HKey_Local_Machine/SOFTWARE/MyReg/MySec下MyValue一项  
  9.     reg.DeleteValue('MyValue');  
  10.   end;  
  11. end;  
  • 删除Key

[delphi] view plaincopyprint?
  1. var  
  2.   reg:TRegIniFile;  
  3. begin  
  4.   reg := TRegIniFile.Create;               //创建实例  
  5.   reg.RootKey := HKey_Local_Machine;       //设置根值  
  6.   if reg.OpenKey('SOFTWARE/MyReg',True) then  
  7.   begin  
  8.     //删除HKey_Local_Machine/SOFTWARE/MyReg下的MySec  
  9.     reg.EraseSection('MySec');  
  10.   end;  
  11. end;  

  • 读取Key的列表以及读取Key下值的列表都与INI文件的操作类似,这里就不再熬述了.
原创粉丝点击