怎样一打开程序就检查注册表某个主键(左边)下是否有某个子键(右边)?或某个子键(右边)是否有某个值?举例说明,45分(45分)

  • 主题发起人 主题发起人 thebluehouse
  • 开始时间 开始时间
T

thebluehouse

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样一打开程序就检查注册表某个主键(左边)下是否有某个子键(右边)?和检查某个子键(右边)是否有某个值?举例说明,45分
比如HKEY_LOCAL_MACHINE/Software/Microsoft/Windows/CurrentVersion/右边是否有ProductId.
其ProductId是否为55660-005-.............?
 
需 uses Registry;
将它放在 Form.Create 中或 Application.Run 之前。
代码:
var 
  ID: String;
  Reg: TRegistry;

Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software/Microsoft/Windows/CurrentVersion', False);
ID := Reg.ReadString(ProductID);
Reg.Free;
 
键和键值是不同的!!!
要这样检查:

以你说的为例:

var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/') then
begin
if ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
ShowMessage(TempValue);
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;
 
你想在程序一运行就检查?可以把我定义的局部TempReg定义成全局的,在Formcreate
里或在Initialization里加入我提供的代码,就可以了,,,

呵呵,,,,简单得嘛,
 
为什么我把ShowMessage(TempValue);换成label1.caption:=TempValue会出错?,还有如何检查有无ProductID健而不是值?
if Tempvalue:=''//空值
为什么出错?
 
呵呵,thebluehouse,你看你的条件中用的符号是什么没? 是 赋值号 哦!!!!!

应该是比较符号“ = ”才对哦,你也太不仔细了吧:)

你把ShowMessage(TempValue);换成label1.caption:=TempValue会出错?出什么错!不会的
哦!

检查键用的是 TempReg.KeyExists(),你要清楚你给我的ProductID是一个值哈,而不是一个
键,键和值有区别的!你莫要混淆了哈!!!!






 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Registry, StdCtrls;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
label1(出错).caption:=tempvalue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end.
就是啊?为什么总是出错?
 
你只回答了一半,如何检查是否有右边ProductId键?我的QQ:108438328
 
出错?我试试!!!
 
没错啊?!!

下面是我整个工程的原码:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,registry, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
TempReg:TRegistry;
TempValue:string;
begin
TempReg:=TRegistry.Create;
try
TempReg.RootKey:=HKEY_LOCAL_MACHINE;
if TempReg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/',false) then
begin
if tempreg.ValueExists('ProductId') then
begin
TempValue:=TempReg.ReadString('ProductId');
label1.caption:=tempvalue;
end
else
ShowMessage('ProductID is not exist!');
end;
finally
TempReg.CloseKey;
TempReg.Free;
end;
end;

end.

我什么都没改,就是把 label1(出错).caption:=tempvalue;
改成了 label1.caption:=tempvalue;

 
如何检查是否有右边ProductId键?

你是检查是“键”还是“值”哦?

检查一个键下是否有子键:用
if tempreg.HasSubKeys then
begin
//Add your code
end;

检查键下面是否有值:

Val:TStringList;

tempreg.GetValueNames(Val);
if Val.count>0 then
begin
//表明该键下面存在值,各个值你可以通过Val.strings[index]来访问!
end;


这下该是回答完全了吧?
 
QQ是多少?我太崇拜您了!我是QQ:108438328
 
接受答案了.
 
后退
顶部