Delphi 7 - How to detect if an ini key exists, if it doesn't create one.

With this code I'm able to read and write in ini files.
Read:
myINI.ReadString('Style', 'Theme', 'Default');
Write:
myINI.Write('Style', 'Theme', 'Default');

However, how can I detect whether 'Style', 'Theme' exists? And if it doesn't create it with the default value ?

Thanks.
John86aAsked:
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.

RezaSadighCommented:
Hi my friend,
Use
  function ValueExists (const Section, Ident: String): Boolean;
Like below for detect whether 'Style', 'Theme' exists?
if myINI.ValueExists ('Style', 'Theme') then
begin
  // Exists
end
else // not Exists
begin
  myINI.Write('Style', 'Theme', 'Default');
end;

Best regards
0
jimyXCommented:
To detect the section only:
if myINI.SectionExists ('Style') then
  showmessage('Section exist')
else
  showmessage('Section does not exist');

Open in new window


To detect the key and the section:
if myINI.ValueExists ('Style', 'Theme') then
  showmessage('Value exist')
else
  showmessage('Value does not exist');

Open in new window


And when you write a value it creates both section and key:
myINI.Write('Style', 'Theme', 'Default');

Open in new window


BUT, when detecting by using "myINI.ValueExists" even if there was no value you will get "True" indicating the value exist but in fact there is no value.
[Style]
Theme=

Open in new window

So to make sure there is always a default value, you can do something like this:
If myINI.ReadString('Style', 'Theme', 'Default') = 'Default' then  // mind the case sensitivity
  myINI.Write('Style', 'Theme', 'Default');

Open in new window

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
jimyXCommented:
Update to my last code snippet in my previous post:
If myINI.ReadString('Style', 'Theme', 'Default') = '' then
  myINI.Write('Style', 'Theme', 'Default');

Open in new window

0
AshokCommented:
if myINI.SectionExists ('Style') then
begin
  if Not myINI.ValueExists ('Style', 'Theme') then
    myINI.Write('Style', 'Theme', 'Default');
end
else
begin
   myINI.Write('Style', 'Theme', 'Default');
end;
// Now Read as usual.....

Ashok
0
John86aAuthor Commented:
thanks guys.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.