本文整理自网络,侵删。
uses //加 registry; 这是第1步
var reg:tregistry; 定义变量这是第2步
Reg := TRegistry.Create 创建一个TRegistry对象这是第3步
{WriteString写入一个字符串值,WriteInteger写入一个整数值}
{ReadString读取一个字符串值,ReadInteger读取一个整数值}
{OpenKey('software',true)键值不存在的时候,则自动创建该键}
{OpenKey('software',false)只打开该键,若不存在则返回空值,并不创建该键}
例子1:实现开机自启动
procedure TForm1.Button1Click(Sender: TObject);
begin
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run',true) then
begin
WriteString('6331905',application.ExeName );
end; {WriteString写入一个字符串值,WriteInteger写入一个整数值}
CloseKey;//关闭主键
free;
end;
end;
//*************************************************************************
例子2:注册表操作
//单项操作例子 ************************************************************
procedure TForm1.Button1Click(Sender: TObject);
begin
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\111',false) then
begin
memo1.lines.add(ReadString('6331905')); //读
end;
CloseKey;//关闭主键
free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\111',true) then
begin
WriteString('6331905',application.ExeName ); //写
end;
CloseKey;//关闭主键
free;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\111',false) then
begin
DeleteValue('6331905'); //删除6331905项
end;
CloseKey;//关闭主键
free;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\',false) then
begin
Deletekey('111'); //删除111目录
end;
CloseKey;//关闭主键
free;
end;
end;
//连续操作例子***********************************************************
/delphi实行一键多项写注册表
procedure TForm1.Button1Click(Sender: TObject);
begin
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\111',true) then
begin
WriteString('555',application.ExeName ); //写
WriteString('666',application.ExeName ); //写
WriteInteger('777',25 ); //写dword值
end;
CloseKey;//关闭主键
free;
end;
//*****************************************************************************
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('SYSTEM\111',true) then
begin
WriteString('555',application.ExeName ); //写
WriteString('666',application.ExeName ); //写
WriteInteger('777',25 ); //写dword值
end;
CloseKey;//关闭主键
free;
end;
//******************************************************************************
reg := TRegistry.Create;
with reg do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey('SYSTEM\111',true) then
begin
WriteString('555',application.ExeName ); //写
WriteString('666',application.ExeName ); //写
WriteInteger('777',25 ); //写dword值
end;
CloseKey;//关闭主键
free;
end;
end;
end.
end;
end;
end.
相关阅读 >>
Delphi 2009 之 tstringbuilder 类[7]: tostring
Delphi net.httpclient用最精简的代码获取网页数据
更多相关阅读请进入《Delphi》频道 >>