正确的关于注册表的代码: 虽然是dephi的,但是原理和VC一样的
Procedure StartReg(yes:Boolean);
Var
reg : TRegistry;
Begin
reg := TRegistry.Create; //建立一个Registry
if Yes=true then
begin
with reg do
begin
RootKey:=HKEY_LOCAL_MACHINE; //设置根键值为HKEY_LOCAL_MACHINE
if OpenKey('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',True)
then WriteString('mailer',ExtractFilePath(Application.Exename)+'mail.exe')
else ShowMessage('创建键值失败'); //创建键值失败
end; //End_With...do
end //End_If True_Begin
else
begin
with reg do
begin
RootKey:=HKEY_LOCAL_MACHINE; //设置根键值为HKEY_LOCAL_MACHINE
if OpenKey('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',True)
then DeleteValue('mailer')
else ShowMessage('删除键值失败'); //键值失败
end; //End_With...do
end; //End_If False_Begin
Reg.CloseKey;
end;
删除一个值前一定要先打开一个键。开始不知道,试了很多次才成功删除,原来删除之前要先打开,我之前使用的是
RegCreateKey来操作的,难怪呢。
注册表操作(目前主要是删除添加操作)
最新推荐文章于 2024-03-03 15:07:02 发布