Board index » delphi » How to make ini files

How to make ini files

Who knows a smart way to implement INI configuration files for Delphi
applications?

The purpose is to store user options in variables. In a preferences
menu the user sets desired options. When the user quits, these must be
written to disk and reloaded the following session.

I am looking for a way that is neat, i.e. not so messy with loads of
variable names floating about. The types needed are boolean, string
and integer. It would be good if one could pack them into an array of
sorts or define a special type.

So far I am thinking of using TStringList.LoadfromFile and
TStringList.SaveToFile.

I want to identify values from the strings in the stringlist and
assign them to their appropriate variables. Can one avoid endless
blocks of if.. else if.. for each and every line of the INI file?

If one had say 100 preference variables, that might causes an average
(100*100)/2 = 5,000 lines of code executed just to interpret the INI
file.

Who knows something better?

Emails appreciated. I will reply

Matthew

 

Re:How to make ini files


Matthew <Com...@lottery.powernet.co.uk> wrote in article
<516h6o$...@power2.power.net.uk>

Quote

> Who knows a smart way to implement INI configuration files for Delphi
> applications?

I think the best way is to use TIniFiles with something like...

uses IniFiles;

procedure ReadConfiguration;
var
     PicodaIni : TIniFile;
begin
     PicodaIni := TIniFile.Create('picoda.ini');
     with PicodaIni do
        begin
          clEditColor := ReadInteger('Configuration', 'EditColor',
clWindow);
          clDisplayColor := ReadInteger('Configuration', 'DisplayColor',
clGrayText);
          iPollFrequency := ReadInteger('Configuration', 'PollFrequency',
1000);
          iNetworkTimeout := ReadInteger('Configuration', 'NetworkTimeout',
2000);
          iNetworkRetries := ReadInteger('Configuration', 'NetworkRetries',
5);
          sDataDirectory := ReadString('Configuration', 'DataDirectory',
'C:\Picoda\Data');
          sProgramDirectory := ReadString('Configuration','BinDirectory',
C:\Picoda\Bin');
          sInterfaceName := ReadString('Network',
'InterfaceName','IDEC-485');
     end;
    PicodaIni.Free;
end;

Re:How to make ini files


Quote
Matthew wrote:

> Who knows a smart way to implement INI configuration files for Delphi
> applications?

> The purpose is to store user options in variables. In a preferences
> menu the user sets desired options. When the user quits, these must be
> written to disk and reloaded the following session.

> I am looking for a way that is neat, i.e. not so messy with loads of
> variable names floating about. The types needed are boolean, string
> and integer. It would be good if one could pack them into an array of
> sorts or define a special type.

> So far I am thinking of using TStringList.LoadfromFile and
> TStringList.SaveToFile.

> I want to identify values from the strings in the stringlist and
> assign them to their appropriate variables. Can one avoid endless
> blocks of if.. else if.. for each and every line of the INI file?

> If one had say 100 preference variables, that might causes an average
> (100*100)/2 = 5,000 lines of code executed just to interpret the INI
> file.

> Who knows something better?

> Emails appreciated. I will reply

> Matthew

Try looking for TIniFile on help file.

Re:How to make ini files


Matthew,

  Have you taken a look at the TIniFile class?

 --- x

Quote
Matthew wrote:

> Who knows a smart way to implement INI configuration files for Delphi
> applications?

> The purpose is to store user options in variables. In a preferences
> menu the user sets desired options. When the user quits, these must be
> written to disk and reloaded the following session.

> I am looking for a way that is neat, i.e. not so messy with loads of
> variable names floating about. The types needed are boolean, string
> and integer. It would be good if one could pack them into an array of
> sorts or define a special type.

> So far I am thinking of using TStringList.LoadfromFile and
> TStringList.SaveToFile.

> I want to identify values from the strings in the stringlist and
> assign them to their appropriate variables. Can one avoid endless
> blocks of if.. else if.. for each and every line of the INI file?

> If one had say 100 preference variables, that might causes an average
> (100*100)/2 = 5,000 lines of code executed just to interpret the INI
> file.

> Who knows something better?

> Emails appreciated. I will reply

> Matthew

Re:How to make ini files


Quote
Matthew wrote:

> Who knows a smart way to implement INI configuration files for Delphi
> applications?

> The purpose is to store user options in variables. In a preferences
> menu the user sets desired options. When the user quits, these must be
> written to disk and reloaded the following session.

> I am looking for a way that is neat, i.e. not so messy with loads of
> variable names floating about. The types needed are boolean, string
> and integer. It would be good if one could pack them into an array of
> sorts or define a special type.

> So far I am thinking of using TStringList.LoadfromFile and
> TStringList.SaveToFile.

> I want to identify values from the strings in the stringlist and
> assign them to their appropriate variables. Can one avoid endless
> blocks of if.. else if.. for each and every line of the INI file?

> If one had say 100 preference variables, that might causes an average
> (100*100)/2 = 5,000 lines of code executed just to interpret the INI
> file.

        Check out TIniFile. (In Windows 95 you're not supposed to be
using ini files any more, btw, you're supposed to be using the registry
instead. That's the official line anyway, I don't quite see what's so
sinful about ini files...)

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:How to make ini files


Quote
"Tom Bowden" <t...@rheyn.com> wrote:
>Matthew <Com...@lottery.powernet.co.uk> wrote in article
><516h6o$...@power2.power.net.uk>

>> Who knows a smart way to implement INI configuration files for Delphi
>> applications?

>I think the best way is to use TIniFiles with something like...

... and if you subclass the TIniFiles to raise some exceptions
when something goes wrong, you will be able to write
a lot of "optimistic" code, and catch the errors in the
except block.

try
   clEditColor := MyIniFile.ReadInteger('Configuration', 'EditColor',
       clWindow);
  ...
except
  on EMyIniFile do
     // Some error handling code
end;

Best Regards,
Frank
--
// Frank Mikalsen, System Developer, Finale a.s      
// Homepage: http://home.sol.no/frankm
// Author of ShareWare: Silent Partner Backup Screensaver v2.60
// Download: http://www.winsite.com/pc/win3/desktop/spbck260.zip

Re:How to make ini files


Quote
Matthew wrote:

< big snip >
> Who knows something better?

> Emails appreciated. I will reply

> Matthew

Goto my Delphi FAQ ( http://www.netcom.net.uk/~rowland/delphi.faq.html
), and have a look at what's there...
--
============================================+===========================
Work:   mailto:Rowl...@rcp.co.uk            | Always be sincere -
Home:   mailto:Rowl...@netcomuk.co.uk       | Whether you mean it or not
Web:    http://www.netcomuk.co.uk/~rowland/ |

Re:How to make ini files


Quote
Matthew wrote:

> Who knows a smart way to implement INI configuration files for Delphi
> applications?

************

I don't think an INI file is the best way to do this. You are probably
better off to store the "Desktop" in an Object Stream file. It depends
on how you organized your program though.

*************

Quote

> The purpose is to store user options in variables. In a preferences
> menu the user sets desired options. When the user quits, these must be
> written to disk and reloaded the following session.

Again - an object file??
********

Quote
> I am looking for a way that is neat, i.e. not so messy with loads of
> variable names floating about. The types needed are boolean, string
> and integer. It would be good if one could pack them into an array of
> sorts or define a special type.

> If one had say 100 preference variables, that might causes an average
> (100*100)/2 = 5,000 lines of code executed just to interpret the INI
> file.

> Who knows something better?

I think that either a plain old DB file or the object storage is the
best approach. C and C++ programmers love the object stream method - but
it's still a good idea!  :-)

The Delphi Informant had a good - but somewhat wrong - article about
objects and storing them in files. They are on the Internet &
Compuserve. See if you can find the code and have a look at it.

We have used object files for storing TAG formats for our Industrial
Tagging program. The objects are streamed out to a file, and read from
the file to print them. Same principle.

--
Regards,
Dave Robinson, VP Development
Amber Computer Systems Inc.

WEBB page: http://mindlink.net/amber_computer/
ASAP-RTS Manufacturing Scheduling and shop floor data collection with
bar coding, and product tagging.  
Databases from directories - an OCR and Scanning system to create
marketing databases from directories.

Re:How to make ini files


Quote
> Check out TIniFile. (In Windows 95 you're not supposed to be using
> ini files any more, btw, you're supposed to be using the registry
> instead. That's the official line anyway, I don't quite see what's so
> sinful about ini files...)

INI files are easier to edit, and thus easier to corrupt or damage.
Personally, as a techie, I love INI files.  Viewing/editing a INI
when trying to solve problems is an excellent educational experience,
and it is frequently the only way to solve a particular problem.

On the other hand, there are many people who will modify an INI file
without making a backup first.  Then there are people who delete all
of the INI files because their hard disk is full and they didn't put
the INI files there to begin with.  Etc...  I assume the Registery
is safer because is not as easy to access or delete (not hard, just
not obvious).

My 2 cents worth, if it's worth that much...

Jon

PS  I still prefer INI files...  Then again, I'm not very tolerent of
    ignorant computer users.  I mean, would you let just anyone drive
    beside you on the highway?  I'd like to think they passed the
    required tests and knew enough to obtain a legal drivers license.

Re:How to make ini files


On 15/09/1996 10:52, in message <51gfr9$...@mn5.swip.net>, Bo Berglund

Quote
<bo.bergl...@mailbox.swipnet.se> wrote:
> What do you think?

> Cheers!

Entirely so. Mostly my apps use INI files...using the TINIFile object. Tis
easier, faster and hell...why not?

The Registry I would only use for very private settings (passwords etc), or
Configuration stuff...If i see what NT stores in the registry...it's disgusting
(lose your registry and here comes the entire re-install).

Stefan

Re:How to make ini files


Quote
David Ullrich <ullr...@math.okstate.edu> wrote:
>Matthew wrote:

>> Who knows a smart way to implement INI configuration files for Delphi
>> applications?

>    Check out TIniFile. (In Windows 95 you're not supposed to be
>using ini files any more, btw, you're supposed to be using the registry
>instead. That's the official line anyway, I don't quite see what's so
>sinful about ini files...)
>--
>David Ullrich

I quite agree about this! It's not sinful, it is downright common sense to
use the INI file method!
If we go from using INI files to using the registry instead we immediately
lose the ability to run our apps in a multiboot environment!

I have a Win95/WinNT machine for development work and have installed
Delphi, MS-Office etc etc in both environments so I can use them all the
time. The install was to the same directories so they share the same
executables. But the snag is this:
Any settings change I make in an app is stored ONLY in the environment it
was made in so when I reboot into the other all changes are lost!
This is because Win95 and WinNT use different registry structures and
files. (WinNT can't even read the Win95 registry when you install NT so
you cannot migrate your apps!)
If on the other hand an app uses INI files stored in the *app dir* (don't
store in the windows dir since that changes on multiboot!) then you can
preserve the settings across operating systems. A much better way in my
opinion.
What do you think?

Cheers!

--------------------------------------------------------
Bo Berglund

(please e-mail to: bo.bergl...@mailbox.swipnet.se)

Re:How to make ini files


Quote
> I quite agree about this! It's not sinful, it is downright common sense
to
> use the INI file method!
> If we go from using INI files to using the registry instead we
immediately
> lose the ability to run our apps in a multiboot environment!
[some more stuff]
> What do you think?

I agree.

Not knowing the techo details of the implementation of the registry, but I
would think that storing a whole lot of junk (such as app exe names, most
recently used file lists etc) in a huge file would be a waste of space, and
slower to extract/store.  How is fragmentation handled in a registry?

INI files are a neat solution to the problem of storing dynamic
information.  I would say however, that INI files should always be stored
in the application directory, along with as many DLLs etc as possible.  I
hate programs which throw INI and DLL files all over my nice pristine OS
directory structure.

--
Mark Pritchard

Re:How to make ini files


Quote
Mark Pritchard wrote:

> > I quite agree about this! It's not sinful, it is downright common sense
> to
> > use the INI file method!
> > If we go from using INI files to using the registry instead we
> immediately
> > lose the ability to run our apps in a multiboot environment!
> [some more stuff]
> > What do you think?

> I agree.

> Not knowing the techo details of the implementation of the registry, but I
> would think that storing a whole lot of junk (such as app exe names, most
> recently used file lists etc) in a huge file would be a waste of space, and
> slower to extract/store.  How is fragmentation handled in a registry?

> INI files are a neat solution to the problem of storing dynamic
> information.  I would say however, that INI files should always be stored
> in the application directory, along with as many DLLs etc as possible.  I
> hate programs which throw INI and DLL files all over my nice pristine OS
> directory structure.

        I agree. BUT the official line is(was) that ini files "must" be stored
in the Windows directory, not in the application directory. The reason is
that in a network environment the Windows directory is the only one you can
be certain exists locally. That's what I read, anyway...

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Go to page: [1] [2]

Other Threads