Solved

problem with tinifile.create

Posted on 1999-01-16
15
777 Views
Last Modified: 2012-05-04
Hi experts,

with the following code I want to create a *.ini file.
Hpowever after running this syntactically correct program, I never
can find the created *.ini file on my harddisk. (The explorer of Windows 98 says "file not found").

Can you please tell me, what I am doing wrong here?

With kind regards

Mathes

unit tini;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IniFiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  sl: tstringlist;
function EnumFiles(path: string; findFile: string): string;
function findFileOnAllHarddisks(fileName: string): string;

implementation

{$R *.DFM}

function EnumFiles(path: string; findFile: string): string;
var
  fd1: TWin32FindData;
  c1: cardinal;
  s1: string;
begin
  if fileExists(path + findFile) then begin
    result := path + findFile;
    exit;
  end;
  c1 := FindFirstFile(PChar(path + '*.*'), fd1);
  if c1 <> INVALID_HANDLE_VALUE then
  try
    repeat
      s1 := string(fd1.cFileName);
      if (s1 <> '.') and (s1 <> '..') and (fd1.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0) then
      begin
        result := EnumFiles(path + s1 + '\', findFile);
        if result <> '' then break;
      end;
    until not FindNextFile(c1, fd1);
  finally Windows.FindClose(c1) end;
end;


function findFileOnAllHarddisks(fileName: string): string;
var
  c1, c2: cardinal;
  ch1: char;
begin
  result := '';
  c2 := GetLogicalDrives;
  for ch1 := 'A' to 'Z' do
    if odd(c2 shr (ord(ch1) - ord('A'))) then begin
      c1 := GetDriveType(PChar(ch1 + ':\'));
      if c1 = DRIVE_FIXED then begin
        result := EnumFiles(ch1 + ':\', fileName);
        if result <> '' then break;
      end;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  myprogIni: TIniFile;
  s: string;
begin
  s := findFileOnAllHarddisks('myprog.exe'); //find location of myprog.exe
  s := extractfilepath(s);
  s := s + 'myprog.ini';                     // in the same directory of myprog.exe...
  myprogIni := TIniFile.Create(s);           // .... create myprog.ini
  myprogIni.Free;
end;

end.
0
Comment
Question by:mathes
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
Try writing something to the INI file, and see if that creates it.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
Hi Mathes,

to access/Create a ini file in the same directory of your Prog with the same name as your Prog, you can use this Statement:

  MyIniFile := TiniFile.Create(ChangeFileExt(Paramstr(0),'.INI'));

meikl
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
Hi Mathes,

If try to create empty txt files on your disk windows is friendly enough to destroy them by default. This is also true for ini files, put some entries and your ini will be saved leave it empty as you've done and it will be destroyed.

MS did a lot to clutter our disks but if we try to do it ourselves they punish us for even trying :O)))).

Hope this helps.
:O)
brUINTje.

0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
Hi Mathes,

Appendix
the INI-File will created (if not exits) by writing something ini it like

MyIniFile := TiniFile.Create(ChangeFileExt(Paramstr(0),'.INI'));   MyIniFile.WriteString('StartParams','MyAppHomeDir',ExtractFilePath(ParamStr(0)));


meikl
0
 

Author Comment

by:mathes
Comment Utility
Dear experts,

thank you for your help. You found the reason why it didn't work.
Obviously this inifile needs some content, but you can't create an
inifile without entries. And the concpt of meikl with changefileext and paramstr
is very interesting, too.

with kind regards

mathes


0
 

Author Comment

by:mathes
Comment Utility
Dear experts,

thank you for your help. You found the reason why it didn't work.
Obviously this inifile needs some content, but you can't create an
inifile without entries. And the concpt of meikl with changefileext and paramstr
is very interesting, too.

with kind regards

mathes


0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
you can find the location of my progexe.exe if the program myprog.exe is your own program by doing this...

var
  path : string;
  MyIniName : string;
begin
  path := ExtractPathName(Application.FileName);
  MyIniName := path = 'myini.ini';
  Ini.Create(myininame);
  ini.free;
end;

-Viktor
--Ivanov
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:philipleighs
Comment Utility
Just one comment about ini files. If you create a new inifile object and write stuff to it, be aware that nothing appears on your hard disk until a different ini file is opened, or you close your app.

This is because Windows caches one ini file at a time.

You can demonstrate this by creating a text file with nothing in it from windows explorer.

Now in code, create an ini file object from the empty file.
Write something using writestring.

Now load the file into a stringlist, observing it is still empty.

Now create a different inifile object, write something to it.
Reload the original ini file into a stringlist, and it will contain your entry.

You only really have to be aware of this if for example you save a project file (say) in an INI format by using AssignFile and WriteLn, but read it in using a TIniFile object. Before loading, write a proc called FlushIniCache or something which opens a dummy ini file and reads a dummy string (the file does not need to exist)

Also, Windows does not delete zero bytes file by default.
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
Hi Phillip,

I do have one question left over, is writing a value with writestring then sufficient for saving a ini file on disk?

See the following code no error checking (use at your own risk :O) )

2 buttons and an edit box on a form

procedure TForm1.Button1Click(Sender: TObject);
var
  myprogIni: TIniFile;
  s  : string;
begin
  s := findFileOnAllHarddisks('myprog.exe'); //find location of myprog.exe
  s := extractfilepath(s);
  s := s + 'myprogral.ini'; // in the same directory of myprog.exe...
  myprogIni := TIniFile.Create(s); // .... create myprog.ini
  myprogIni.WriteString('Path of my program', 'program-path', 'c:\mypath.ini');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  winposhIni: TIniFile;
  i, position: integer;
  temp: string;
  sl : tstringlist;
begin
  if fileexists('c:\myprogral.ini') then begin
    myprogIni := TIniFile.Create('c:\myprogral.ini');
    sl := tstringlist.create;
    myprogIni.ReadSectionValues('Path of my program', sl);
    for i := 0 to sl.count - 1 do
      temp := sl.strings[i];
    position := pos('=', temp);
    delete(temp, 1, position);
    edit1.text := temp;
    myprogIni.Free;
    sl.free;
  end;
end;

When I leave out the writestring in button1click I don't get a ini file in the explorer, whereas I use writestring I do get it saved to use in the button2click, that's where my conclusion came from that windows is removing the empty file itself.

Always curious :O?
brUINTje.

0
 
LVL 3

Expert Comment

by:philipleighs
Comment Utility
Ah, I see what you are saying.

TIniFile.Create('myfile.ini') does not create a file at all.

Here is the source code for the constructor:

constructor TCustomIniFile.Create(const FileName: string);
begin
  FFileName := FileName;
end;

Here is the code for WriteString. It is the Win API call to WritePrivateProfileString that actually creates the file. It uses FFileName which was set in the constructor.

procedure TIniFile.WriteString(const Section, Ident, Value: string);
begin
  if not WritePrivateProfileString(PChar(Section), PChar(Ident),
    PChar(Value), PChar(FFileName)) then
    raise Exception.CreateFmt(SIniFileWriteError, [FileName]);
end;


0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
Thanks Philip :O)
0
 
LVL 3

Expert Comment

by:philipleighs
Comment Utility
Is that enough to get an A graded answer from you?
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
OKidOKi
Think it's fair enough to put it up for say.......... 20 pts.
:O)
0
 
LVL 3

Accepted Solution

by:
philipleighs earned 10 total points
Comment Utility
OK Thanks!
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
Hmm...
Philip I did put up a seperate Q for your answer to my question in this thread hahahahahahhahaa
Should look at the "for Philip" Question in the Delphi area it's rated for 20 pts and when you answer you'll get an A

:O) Always happy!
Bruintje
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now