ple103
ple103

Reputation: 2110

.ini file not creating

I have begun to start working with .ini files because I realise its many advantages compared to a .txt file. Any way, I googled "delphi inifiles" and I am now following a guide on Delphi About. However the very first line I tried gave me trouble even though all the syntax is right.

Delphi About's Code:

IniFile := TIniFile.Create('myapp.ini') ;

My code:

IniFile := TIniFile.Create('SWEDISH_HOUSE_MAFIA.ini');

The only difference is the name of the .ini file itself. Also, yes I have:

I have placed that code under FormCreate, but the problem is this .ini file is not created when the program is run. Anyone know what the problem could be? I've asked a friend about it and he said it was a permission issue.

Extra Details:

Upvotes: 3

Views: 11077

Answers (4)

Jp Silver
Jp Silver

Reputation: 11

TForm1.btnWriteClick(Sender: TObject); 

var

    myINI : TINIFile;

begin 

    myINI := TINIFile.Create('Tryfolder\myini.ini');

    myINI.WriteString('Settings', 'Text Box', 'Whatever text');

    myINI.Free;

end;

Like answer 3 said, you just need to write a value to the ini :) This worked for me (win 7 ultimate 64 bit).

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612794

The first issue is that the file is only created when you write something to the ini file. I suspect that at present you aren't calling one of the WriteXXX methods.

The other issue is that if you don't qualify your path then TIniFile will attempt to locate it in the Windows directory and of course you don't have rights to write there. The underlying API that TIniFile is based on is the private profile API which has been long deprecated, performs terribly, and is full of strange wrinkles. The documentation states:

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.

Clearly you should fully qualify your path.

However, I strongly recommend that you consider using TMemIniFile rather than TIniFile since TMemIniFile avoids all the pitfalls of the private profile API.

If you do switch to TMemIniFile then remember to call UpdateFile before destroying the ini file since this is what will save the settings to the disk. Otherwise TMemIniFile is a drop-in replacement for TIniFile.

Upvotes: 17

DiGi
DiGi

Reputation: 2548

  1. Don't save ini to application path. Use %APPDATA% path. INI without path is created in Windows folder (Win3..XP). On Vista/7 these files are redirected to \Users\[user]\some-magic-folder (unless your program runs as "Installer")
  2. Using TMemFile will speedup operations a lot. You need call UpdateFile/Free to update file, but it is much faster

Upvotes: 2

LU RD
LU RD

Reputation: 34889

To make sure the file is actually saved on disk, use

IniFile.UpdateFile

Upvotes: 2

Related Questions