• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Read registry key value

I want to read an EXISTED registry key value. I used following routine:

SetReg := TRegistry.Create;
SetReg.OpenKey('HKEY_LOCAL_MACHINE\SOFTWARE\mysoftware\app1', True);
SerialNum := SetReg.ReadString('SerialNumber');

Somehow, the value is not read and a new registry key named 'HKEY_LOCAL_MACHINE\Software\mysoftware\app1'is created.

How should I solve the problem?

Thank you!
0
llucy88
Asked:
llucy88
  • 4
  • 3
  • 2
  • +1
1 Solution
 
philipleighsCommented:
Try this:

if SetReg.OpenKeyReadOnly('HKEY_LOCAL_MACHINE\SOFTWARE\mysoftware\app1') then
begin
  ...
end;
0
 
philipleighsCommented:
PS: You need D4. If you have D3 then use OpenKey but pass False instead of true.

Cheers,
Phil.
0
 
Stuart_JohnsonCommented:
Hi.

To stop the key from being created if it does not exist, use this:

SetReg.OpenKey('SOFTWARE\mysoftware\app1', False);

(note the false parameter)

You should then use:

If SetReg.ValueExists('SerialNummber') then SerialNum := SetReg.ReadString('SerialNumber');

You also must specify the root key (HKEY_LOCAL_MACHINE).    

So, your code would look like

var
   SetReg: TRegistry;
begin
   SetReg := TRegistry.Create;
   SetRet.RootKey := HKEY_LOCAL_MACHINE;
   If SetKey.OpenKey('Software\MySoftware\App1', False) then
      if SetKey.ValueExists('SerialNumber') then
         SerialNum := SetKey.ReadString('SerialNumber');
   SetKey.Close;
   SetKey.Free;
End;

I hope this helps you out.  Let me know If you need more info.


Cheers,


Stu.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
she3i3iCommented:
Listening...
0
 
llucy88Author Commented:
It is working, Thank you very much!
0
 
philipleighsCommented:
OK, glad it's working for you.

You should close this thread off now by accepting either Stu's or my comment as an answer.

Cheers,
Phil.
0
 
Stuart_JohnsonCommented:
Excellent.  Which method did you end up using?  Care to share?

Cheers,

Stu.
0
 
llucy88Author Commented:
Both of your comments helped me out though I coded using stu's way.

Thank you all very much!

llucy88
0
 
philipleighsCommented:
OK Stu, points are yours.  :-)

Cheers,
Phil.
0
 
Stuart_JohnsonCommented:
Thanks, llucy88.  Thanks Phil.

Have a nice day both!

Stu.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now