c-prompt
asked on
INI Files
What are INI files for and are they used in Delphi applications? Could you store info in these files and load them into your program, if so could someone give me an example on how to set that up?
The INI files format is a bit antiquated mechanism for storing a reasonable small amount of configuration data. It's biggest advantage is that it can be edited by using Notepad, thus you can make changes without starting the associated application. INI files are still pretty popular since they're easy to use and reasonable fast. Especially since INI files are divided in sections and every section has a key-value pair, they can have quite a few uses.
You could use the registry instead but here lies a minor problem. If your application is installed in a network environment, and needs default settings for all the PC's connected to it, storing this settings in the registry would just bring total chaos. Because every user will have his own set of registry settings, every user would need to know the default values and changing them globally is just impossible. Thus an INI file is useful for global configuration data while the registry is just a more personalized configuration set.
But at this moment, XML seems to become a new standard for storing configurations. Especially because XML is more like a tree with lots of levels while an INI file is just a list of key-values in a list of sections.
For the ise of Inifiles, see the IniFiles unit. The use is somewhat like this:
var IniFile: TInifile;
begin
IniFile := TIniFile.Create('YourINIFi le.ini');
try
IniFile.WriteString('Secti on','Key', AValue);
AValue := IniFile.ReadString('Sectio n','Key',' DefaultVal ue');
finally
IniFile.Free;
end;
end;
That's writing and reading data... Integers in this case. See the helpfiles for more info.
You could use the registry instead but here lies a minor problem. If your application is installed in a network environment, and needs default settings for all the PC's connected to it, storing this settings in the registry would just bring total chaos. Because every user will have his own set of registry settings, every user would need to know the default values and changing them globally is just impossible. Thus an INI file is useful for global configuration data while the registry is just a more personalized configuration set.
But at this moment, XML seems to become a new standard for storing configurations. Especially because XML is more like a tree with lots of levels while an INI file is just a list of key-values in a list of sections.
For the ise of Inifiles, see the IniFiles unit. The use is somewhat like this:
var IniFile: TInifile;
begin
IniFile := TIniFile.Create('YourINIFi
try
IniFile.WriteString('Secti
AValue := IniFile.ReadString('Sectio
finally
IniFile.Free;
end;
end;
That's writing and reading data... Integers in this case. See the helpfiles for more info.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
TIniFile.Create(IniFileNam e);
if IniFileName is NOT a full path name, TIniFile will search it in windows home directory (such as c:\windows, windows9x/me/xp or c:\winnt , win2000) by default. if the file exist then open else create it automatically.
adayuer
if IniFileName is NOT a full path name, TIniFile will search it in windows home directory (such as c:\windows, windows9x/me/xp or c:\winnt , win2000) by default. if the file exist then open else create it automatically.
adayuer
adayuer, what you said is not really true...
INI file can be in any folder you wish. Actually the application search for it in the current folder and folders in PATH. That's why it better to give the full path. Because if you say just CONFIG.INI and it is in your application folder, when the current directory is changed, it won't be found anymore.
That's why giving the full path will save you troubles. Putting you configuration files in Windows folder is a little shitty, since WINDOWS folder don't belong to you. Thus you are making garbage there.
INI file can be in any folder you wish. Actually the application search for it in the current folder and folders in PATH. That's why it better to give the full path. Because if you say just CONFIG.INI and it is in your application folder, when the current directory is changed, it won't be found anymore.
That's why giving the full path will save you troubles. Putting you configuration files in Windows folder is a little shitty, since WINDOWS folder don't belong to you. Thus you are making garbage there.
to Ivanov G:
hi, I found some source code in Delphi Source Code like this:
TCustomIniFile = class(TObject)
private
FFileName: string;
public
constructor Create(const FileName: string);
....
property FileName: string read FFileName;
end;
constructor TCustomIniFile.Create(cons t FileName: string);
begin
FFileName := FileName;
end;
TIniFile = class(TCustomIniFile)
public
destructor Destroy; override;
....
procedure UpdateFile; override;
end;
destructor TIniFile.Destroy;
begin
UpdateFile;
inherited Destroy;
end;
procedure TIniFile.UpdateFile;
begin
WritePrivateProfileString( nil, nil, nil, PChar(FFileName));
end;
then I found "WritePrivateProfileString " detail information in MSDN, like this:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
about lpFileName, in Remarks section, there have some words : "If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.
If lpFileName contains a full path and file name and the file does not exist, WritePrivateProfileString creates the file. The specified directory must already exist. "
ok ,all things became clear, the core of this question is TIniFile.UpdateFile procedure. this procedure call the win api "WritePrivateProfileString " , TIniFile.Create()'s paramter is the WritePrivateProfileString' s last paramter - lpFileName.
"WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory." is my answer's base.
for more information, I wrote code :
var
ini : TIniFile ;
begin
ini := TIniFile('test.ini');
ini.WriteString('test', 'name', 'value') ;
ini.Free ;
end;
when i run these codes, i found the file "test.ini" in Windows Home Direcotry, i removed "test.ini" from Windows Home Directory to my App Current Directory. then i change my code :
ini.WriteString('test', 'name', 'value1') ;
run again, I found new "test.ini" created in Windows Home Directory, it's content :
[test]
name=value1
i open "test.ini" in my app Directory, the content:
[test]
name=value
the application do NOT search for the ini file in OTHER folder(s) except Windows Directory otherwise you give it a full path.
I agree with you about "Putting you configuration files in Windows folder is a little shitty, since WINDOWS folder don't belong to you. Thus you are making garbage there."
08 15, 2004
Regards, adayuer
hi, I found some source code in Delphi Source Code like this:
TCustomIniFile = class(TObject)
private
FFileName: string;
public
constructor Create(const FileName: string);
....
property FileName: string read FFileName;
end;
constructor TCustomIniFile.Create(cons
begin
FFileName := FileName;
end;
TIniFile = class(TCustomIniFile)
public
destructor Destroy; override;
....
procedure UpdateFile; override;
end;
destructor TIniFile.Destroy;
begin
UpdateFile;
inherited Destroy;
end;
procedure TIniFile.UpdateFile;
begin
WritePrivateProfileString(
end;
then I found "WritePrivateProfileString
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
about lpFileName, in Remarks section, there have some words : "If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.
If lpFileName contains a full path and file name and the file does not exist, WritePrivateProfileString creates the file. The specified directory must already exist. "
ok ,all things became clear, the core of this question is TIniFile.UpdateFile procedure. this procedure call the win api "WritePrivateProfileString
"WritePrivateProfileString
for more information, I wrote code :
var
ini : TIniFile ;
begin
ini := TIniFile('test.ini');
ini.WriteString('test', 'name', 'value') ;
ini.Free ;
end;
when i run these codes, i found the file "test.ini" in Windows Home Direcotry, i removed "test.ini" from Windows Home Directory to my App Current Directory. then i change my code :
ini.WriteString('test', 'name', 'value1') ;
run again, I found new "test.ini" created in Windows Home Directory, it's content :
[test]
name=value1
i open "test.ini" in my app Directory, the content:
[test]
name=value
the application do NOT search for the ini file in OTHER folder(s) except Windows Directory otherwise you give it a full path.
I agree with you about "Putting you configuration files in Windows folder is a little shitty, since WINDOWS folder don't belong to you. Thus you are making garbage there."
08 15, 2004
Regards, adayuer
God bless me, if you couldn't understand my poor english, so...so...so...so...I am CRAZY...
good wishs to you. :)
good wishs to you. :)
For more information look at
http://delphi.about.com/library/weekly/aa120401a.htm