TRegIniFile会简单一些。
98,95,2k都一样。
procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegIniFile;
begin
if Length(NameofKey.Text) or Length(ValueforKey.Text) <=0 then
Showmessage('Either the key name or value is missing.')
else begin
Reg:=TRegIniFile.Create('MyApp');
try
Reg.RootKey:=HKey_Local_Machine; // Section to look for within the registry
if not Reg.OpenKey(NameofKey.Text,False) then
if MessageDlg('The specified key does not exist, create it?' ,Mtinformation,[mbYes,mbNo],0)=mryes then
begin
Reg.CreateKey(NameofKey.Text);
if not Reg.OpenKey(NameofKey.Text,False) then
ShowMessage('Error in Opening Created Key')
else
Reg.WriteString('Main Section','Value1',ValueForKey.Text);
end
else
Reg.WriteString('Main Section','Value1',ValueForKey.Text);
finally Reg.Free;
end;
end;
end;
98,95,2k都一样。
procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegIniFile;
begin
if Length(NameofKey.Text) or Length(ValueforKey.Text) <=0 then
Showmessage('Either the key name or value is missing.')
else begin
Reg:=TRegIniFile.Create('MyApp');
try
Reg.RootKey:=HKey_Local_Machine; // Section to look for within the registry
if not Reg.OpenKey(NameofKey.Text,False) then
if MessageDlg('The specified key does not exist, create it?' ,Mtinformation,[mbYes,mbNo],0)=mryes then
begin
Reg.CreateKey(NameofKey.Text);
if not Reg.OpenKey(NameofKey.Text,False) then
ShowMessage('Error in Opening Created Key')
else
Reg.WriteString('Main Section','Value1',ValueForKey.Text);
end
else
Reg.WriteString('Main Section','Value1',ValueForKey.Text);
finally Reg.Free;
end;
end;
end;
if 访问(hkey_Local_machine) and (user <> Administrator)and (uses TRegistry)
没有95的编程经验
98随便with TRegister.Create do
try
RootKey := HKEY_....
if OpenKey('childkey', false) then
begin
S := ReadString('keyname');
writeString('KeyName', Value);
...
end;
CloseKey;
finally
Free;
end;
REGISTRY;
var
reg:Tregistry;
begin
reg:=Tregistry.create;
reg.rootkey:=HKey_Local_Machine;
Reg.OpenKey(NameofKey.Text,False);//打开;
REG.WRITESTRING('SDFDSA','SDAFSA');//写
REG.REG.ReadString('SDFA');//读
读用API来读,写是不成的.因为没有权限.const
SOpenKey = 'SoftWare\MySetting;
SNode = 'NodeValue';
HRootKey = HKEY_Local_Machine;procedure RaiseError(ErrorCode: Integer);
begin
if ErrorCode <> Error_Success then
raise Exception.Create(SysErrorMessage(ErrorCode));
end;procedure TForm1.Button1Click(Sender: TObject); function GetDataSize(AKey: HKey; AValueName: string): Integer;
var
DataType: Integer;
begin
DataType := 0;
if RegQueryValueEx(AKey, PChar(AValueName), nil, @DataType, nil,
@Result) <> ERROR_SUCCESS then Result := -1;
end;var
Key: HKey;
ErrorCode: Integer;
BufSize, DataType: Integer;
Buffer: Pointer;
S: string;
begin
DataType := Reg_SZ;
ErrorCode := RegOpenKeyEx(HRootKey, PChar(SOpenKey), 0, Key_Read, Key);
RaiseError(ErrorCode);
try
BufSize := GetDataSize(Key, SNode);
ErrorCode := RegQueryValueEx(Key, PChar(SNode),
nil, @DataType, PByte(Buffer), @BufSize);
RaiseError(ErrorCode);
S := PChar(Buffer);
SetLength(S, BufSize);
MessageBox(Handle, PChar(S), '', MB_OK);
finally
RegCloseKey(Key);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
const
SValue= 'Hello'; function WriteString(AKey: HKey; Name: string; Buffer: Pointer; Len: Integer): Integer;
var
DataType: Integer;
begin
DataType := Reg_SZ;
Result := RegSetValueEx(AKey, PChar(Name), 0, DataType, Buffer, Len);
end;var
ErrorCode: Integer;
Key: HKey;
begin
ErrorCode := RegOpenKeyEx(HRootKey, PChar(SOpenKey), 0, Key_Write, Key);
RaiseError(ErrorCode);
try
ErrorCode := WriteString(Key, SNode, PChar(SValue), Length(SValue) + 1);
RaiseError(ErrorCode);
finally
RegCloseKey(Key);
end;
end;