Delphi中对注册表的操作一般为以下步骤:
1. 在uses中加入Registry
例如:
unit unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,Registry; //<-- 加入
2. 定义变量类型为TRegistry
例如:
var
temp:TRegistry;
...
3. 创建变量
例如:
...
temp:=TRegistry.create;
...
4. 修改TRegistry类型变量的Rootkey(根键值)属性, 缺省情况下, 当创建了一个TRegisrty类型
变量时,它的Rootkey属性等于HKEY_CURRENT_USER, 如对于上例: 在执行完temp:=TRegistry.create
后, temp.rootkey就等于HKEY_CURRENT_USER.
在Windows中主要包括以下根键值:
HKEY_CLASS_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USER
HKEY_CURRENT_CONFIG
HKEY_DYN_DATA
你可以根据你的需要来设定根键值:
例如: 要对HKE_LOCAL_MACHINE根键值进行操作,即:
...
temp.rootkey:=HKEY_LOCAL_MACHINE;
...
5. 设定完根键值之后, 要打开具体子键以对其进行操作,这一步用来openkey实现
例如:
var
temp:TRegistry;
begin
temp:=TRegistry.create;
try
with temp do
begin
rootkey:=HKEY_LOCAL_MACHINE;
openkey('Software/Microsoft/Windows/CurrentVersion/Policies/Explorer',false);
...
end;
finally
temp.free
end;
对openkey函数的第二个参数(boolean类型)要注意: 为true: 打开子键, 如该子键不存在则建立之
为false:不建立.
6. 进行基本操作
基本操作包括: 判断键值是否存在---valueexists
读写键值-----------般常用的有:
readinteger writeinteger
readstring writestring
readbinarydata writebinarydata
readfloat writefloat
其余的请看Delphi帮助
例如:
var
temp:TRegistry;
i:string;
begin
temp:=TREgistry.create;
try
with temp do
begin
rootkey:=HKEY_LOCAL_MACHINE;
openkey('Config/0001/Display/Settings',false);
if valueexists('BitsPerPixel') then
//该键值存在
i:=readstring('BitsPerPixel')
else //该键值不存在
...
end;
finally
temp.free
end;
...