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!
llucy88Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.