function TRegOperate.WriteReg(temp_regData:TRegData):boolean; var reg:TRegistry;//声明一个TRegistry类变量 begin result:=false; if strcomp(pchar(temp_regData.Key),pchar(''))=0 then exit; if strcomp(pchar(temp_regData.Name),pchar(''))=0 then exit; if strcomp(pchar(temp_regData.Value),pchar(''))=0 then exit; reg:=TRegistry.Create;//创建实例 reg.RootKey:=HKEY_LOCAL_MACHINE;//指定需要操作的注册表的主键 if reg.OpenKey('\software\'+temp_regData.Key,true)=false then begin if reg.CreateKey('\software\'+temp_regData.Key)=false then exit; end; if reg.OpenKey('\software\'+temp_regData.Key,true)then begin reg.WriteString(temp_regData.Name,temp_regData.Value);//将需要保存的信息写入注册表 reg.CloseKey;//关闭注册表 end; reg.Free;//释放变量所占内存 result:=true;
var reg:TRegIniFile; begin reg := TRegIniFile.Create; //创建实例 reg.RootKey := HKey_Local_Machine; //设置根值 if reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True) then //打开 HKey_Local_Machine\SOFTWARE\MyReg,如果MyReg不存在,则自动创建 begin reg.WriteString('a','a','a'); end; end;
procedure SetStrToReg(RootKey: HKEY; const StrPath, StrName, StrData: PChar);
var
TempKey: HKEY;
Disposition, DataSize: LongWord;
begin
TempKey := 0;
RegCreateKeyEx(RootKey, StrPath, 0, nil, 0, KEY_ALL_ACCESS, nil, TempKey, @Disposition);
DataSize := Length(StrData) + 1; //
RegSetValueEx(TempKey, StrName, 0, REG_SZ, StrData, DataSize);
RegCloseKey(TempKey);
end;
这里有个比较简单的函数
SetStrToReg(HKEY_LOCAL_MACHINE,'Software\QQPet','','');
只写主键
SetStrToReg(HKEY_LOCAL_MACHINE,'Software\QQPet','test','1111');
写主键,并下面生成一个新的键值。
var
reg:TRegistry;//声明一个TRegistry类变量
begin
result:=false; if strcomp(pchar(temp_regData.Key),pchar(''))=0 then exit;
if strcomp(pchar(temp_regData.Name),pchar(''))=0 then exit;
if strcomp(pchar(temp_regData.Value),pchar(''))=0 then exit; reg:=TRegistry.Create;//创建实例
reg.RootKey:=HKEY_LOCAL_MACHINE;//指定需要操作的注册表的主键
if reg.OpenKey('\software\'+temp_regData.Key,true)=false then
begin
if reg.CreateKey('\software\'+temp_regData.Key)=false then exit;
end;
if reg.OpenKey('\software\'+temp_regData.Key,true)then
begin
reg.WriteString(temp_regData.Name,temp_regData.Value);//将需要保存的信息写入注册表
reg.CloseKey;//关闭注册表
end; reg.Free;//释放变量所占内存
result:=true;
end;
楼主仔细看看我的贴,就一个函数,直接贴进代码,不用加其他的单元,而且我还给了你两个使用示例啊。
这里有个比较简单的函数
SetStrToReg(HKEY_LOCAL_MACHINE,'Software\QQPet','','');只写主键
SetStrToReg(HKEY_LOCAL_MACHINE,'Software\QQPet','test','1111');
比如你想写HKEY_LOCAL_MACHINE\Software\xxxxTest, 直接改成SetStrToReg(HKEY_LOCAL_MACHINE,'Software\xxxxTest','','') 就可以写成功。
var
reg:TRegIniFile;
begin
reg := TRegIniFile.Create; //创建实例
reg.RootKey := HKey_Local_Machine; //设置根值
if reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True) then //打开 HKey_Local_Machine\SOFTWARE\MyReg,如果MyReg不存在,则自动创建
begin
reg.WriteString('a','a','a');
end;
end;