我有一個Delphi XE2項目與註冊表項一起工作。所以我定義了下面的代碼:如何讀取註冊表項的默認值
procedure TMainForm.BitBtn01Click(Sender: TObject);
var
RegistryEntry: TRegistry;
begin
RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
RegistryEntry.RootKey:= HKEY_LOCAL_MACHINE;
if (not RegistryEntry.KeyExists('Software\MyCompanyName\MyName\')) then
begin
RegistryEntry.Access:= KEY_WRITE or KEY_WOW64_64KEY;
RegistryEntry.OpenKey('Software\MyCompanyName\MyName\',True);
RegistryEntry.WriteString('', 'MyFirstProject');
end
else
begin
Memo01.Lines.Add(RegistryEntry.ReadString('(Default)')); //Not Working
Memo01.Lines.Add(RegistryEntry.ReadString('')); //Not Working
Memo01.Lines.Add(RegistryEntry.ReadString('@')); //Not Working
end;
RegistryEntry.CloseKey();
RegistryEntry.Free;
end;
我的要求是,以檢查「MYNAME」的默認值,並在Memo01顯示。但沒有什麼是發生。所以我嘗試了另一種方法如下:
procedure TMainForm.BitBtn01Click(Sender: TObject);
var
RegistryEntry: TRegistry;
RegistryString: string;
begin
RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
RegistryEntry.RootKey:= HKEY_LOCAL_MACHINE;
if (not RegistryEntry.KeyExists('Software\MyCompanyName\MyName\')) then
begin
RegistryEntry.Access:= KEY_WRITE or KEY_WOW64_64KEY;
RegistryEntry.OpenKey('Software\MyCompanyName\MyName\',True);
RegistryEntry.WriteString('', 'MyFirstProject');
end
else
begin
RegistryString := RegistryEntry.ReadString('(Default)'); //Not Working
RegistryString := RegistryEntry.ReadString(''); //Not Working
RegistryString := RegistryEntry.ReadString('@'); //Not Working
Memo01.Lines.Add(RegistryString);
end;
RegistryEntry.CloseKey();
RegistryEntry.Free;
end;
但它也不起作用。
問:你有沒有試過RegQueryValueEx('')?一個空字符串('''')應該是你所需要的。問:你確定這個特定的鍵*有一個默認值嗎?你可以在RegEdit中看到它嗎? – paulsm4 2013-04-23 18:41:19
不,我還沒有試過RegQueryValueEx。是的,一切都是關於**默認值**。 – 2013-04-23 18:45:21
但是,@Rubi,我告訴過你[昨天](http://stackoverflow.com/questions/16144046/how-to-set-value-of-registry-key#comment23064639_16144046)。兩次。 – 2013-04-23 18:48:34