Link to home
Start Free TrialLog in
Avatar of pr2501
pr2501

asked on

Wor with ini file


With :
with TINIFile.Create(ExtractPath(Form1.caption)+'.ini')
i would like to work in:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
procedure TForm1.FormCreate(Sender: TObject);

i need to get path where initialization of my form is stored. And i get it from the Form caption.

Example:  myform      The archive is located in: C:\myform
Function TForm1.ExtractPath(Str:String):String;
Var
 P:Integer;
begin
 P:=Pos(':\',Str);
 if P>0 Then Result:=Copy(Str,P-1,Length(Str)) Else Result:='';
end;

Open in new window

Avatar of cebasso
cebasso
Flag of United States of America image

hey, i don't understand what you really want...
but, the variable "Application.Exename" will return where your application was initialized and ExtractFilePath, ExtractFileName can be used from the unit "SysUtils"
if "Application.Exename" return where your app was initialized, so of course is the same path where your form was
why not use something like
with TIniFile.Create(ExtractFilePath(Application.Exename) + 'yourini.ini') do
...
change "yourini.ini" to the name of the ini file that you desire
i'm sorry if i was stupid, maybe its not what you want hehe
procedure TForm1.FormCreate(Sender: TObject);
begin
...
...
Form1.Caption := ExtractFilePath(Application.ExeName);
...
...
end;
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501
pr2501

ASKER



cebasso:

Form caption: myform      The archive is located in: C:\myform

How can i get  for "yourini"  "myform" from line (form caption) above?
with TIniFile.Create(ExtractFilePath(Application.Exename) + 'yourini.ini')


epasquier:
maybe this is argument for another question: but is it posible to save into ini file name of app:
myapp.exe  ?
hi;
try my code;


uses IniFiles

var
  Inifile : tinifile;
  Fname:string;

//procedure TForm1.FormCreate(Sender: TObject);

procedure WriteIniFile;
begin
...
...
Fname := ExtractFileName(Application.ExeName);
...
...
IniFile:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'\'+Fname+'.ini');
IniFile.WriteString('FormCaption','Form1', Fname );
...
IniFile.Free;
end;



procedure ReadIniFile;
begin
...
...
IniFile:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'\'+Fname+'.ini');
Form1.Caption := IniFile.ReadString('FormCaption','Form1','');
...
IniFile.Free;
end;

Open in new window

> is it posible to save into ini file name of app: myapp.exe  ?
sure

IniFile.WriteString('APPLICATION','NAME',ExtractFileName(Application.ExeName));
IniFile.WriteString('APPLICATION','PATH',ExtractFilePath(Application.ExeName));

but what is that useful for ? Is your Ini file used by more than one application ?
hi Epasquier

I think there is a little difference in our code, you uses a direct point method, while I used a step-up method, and I hope this one's is good for him.
Avatar of pr2501

ASKER

When i click on a button while app is runing i get in tedit>


C:\test2\test2.ini
with>
procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Text:= (ExtractPath(form1.caption)+'\'+ExtractPathIni(form1.caption)+'.ini');
end;
from attached picture.
 
but whay i do not get anything whwn i restart my app   with >
procedure TForm1.FormCreate(Sender: TObject);
......
with TINIFile.Create(ExtractPath(form1.caption)+'\'+ExtractPathIni(form1.caption)+'.ini')  do
caption:= ReadString(Self.Name, 'Caption', Caption);
   edit1.text:=  form1.ExtractPath(form1.caption);........
 
and if i look into ini file i can find ewerything it in?
 

Function TForm1.ExtractPath(Str:String):String;
Var
 P:Integer;
begin
 P:=Pos(':\',Str);
 if P>0 Then Result:=Copy(Str,P-1,Length(Str)) Else Result:='';
end;

Function TForm1.ExtractPathIni(Str:String):String;
Var
 PI:Integer;
begin
 PI:=Pos(':\',Str);
 if PI>0 Then Result:=Copy(Str,PI+2,Length(Str)) Else Result:='';
end;

Open in new window

arhive.JPG
caption:= ReadString(Self.Name, 'Caption', Caption); ?

Why not;

Write;
IniFile.WriteString('APPLICATION',  'Caption',  ExtractFileName(Application.ExeName));

Read;
caption := ReadString('APPLICATION',  'Caption',  Caption);  ?


And there is no, undeclared identifier.
form1.ExtractPath
Avatar of pr2501

ASKER

Because i do not need to put ini file on desktop where my app.exe is located.

When i work with
...
with TINIFile.Create('C:\my.ini') do
....
in form close and form create , everything is fine.

Now i need that my ini file is located in "test2".
Look at atached picture above.

And i can get path to "test2" from form caption with atached code.
Function TForm1.ExtractPath(Str:String):String;
Var
 P:Integer;
begin
 P:=Pos(':\',Str);
 if P>0 Then Result:=Copy(Str,P-1,Length(Str)) Else Result:='';
end;

Function TForm1.ExtractPathIni(Str:String):String;
Var
 PI:Integer;
begin
 PI:=Pos(':\',Str);
 if PI>0 Then Result:=Copy(Str,PI+2,Length(Str)) Else Result:='';
end;

Open in new window

Avatar of pr2501

ASKER

But there must be somtheing that  i'm missing, because when i reopen my forme it's empty.
Avatar of pr2501

ASKER


procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Text:= (ExtractPath(form1.caption)+'\'+ExtractPathIni(form1.caption)+'.ini');
end;
>>Because i do not need to put ini file on desktop where my app.exe is located.
You can put your .ini the location where your app.exe is.
eq.,
c:\yourapplocation\app.exe
c:\yourapplocation\app.ini


There is no need of your "function ExtractPath" because delphi has already the function ExtractFilePath.
Then for advice, do not put your app.exe on the desktop, put a shortcut path of your app.exe instead.
here is some code;
https://www.experts-exchange.com/questions/20406985/creating-shortcuts-in-delphi.html
http://delphi.about.com/od/windowsshellapi/l/aa072704a.htm
https://www.experts-exchange.com/questions/24004866/Delphi-Desktop-icons.html
https://www.experts-exchange.com/questions/20515098/Creating-shortcuts-on-desktop-at-runtime.html


The code as I understand your need;

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Button2: TButton;
    Button3: TButton;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    //Function ExtractPath(Str:String):String;
    //Function ExtractPathIni(Str:String):String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  IniFile: TiniFile;

implementation

{$R *.dfm}

//you dont need this
{
Function TForm1.ExtractPath(Str:String):String;
Var
 P:Integer;
begin
 P:=Pos(':\',Str);
 if P>0 Then Result:=Copy(Str,P-1,Length(Str)) Else Result:='';
end;
Function TForm1.ExtractPathIni(Str:String):String;
Var
 PI:Integer;
begin
 PI:=Pos(':\',Str);
 if PI>0 Then Result:=Copy(Str,PI+2,Length(Str)) Else Result:='';
end;
}

procedure TForm1.FormCreate(Sender: TObject);
begin
//
IniFile:=Tinifile.Create(ExtractFilePath(Application.ExeName) + '\' + 'my.ini');
Button1.Caption:='Get_Ini_Path';
Button2.Caption:='Write_Ini';
Button3.Caption:='Read_Ini';
Edit1.Text :='';
Edit2.Text :='';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text:= ExtractFilePath(Application.ExeName) + 'app.ini';
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
//
IniFile.WriteString('APPLICATION','NAME',ExtractFileName(Application.ExeName));
IniFile.WriteString('APPLICATION','PATH',ExtractFilePath(Application.ExeName));
IniFile.WriteString('APPLICATION','INI', ExtractFilePath(Application.ExeName) + 'app.ini');
showmessage('ini file was written in path ' + ExtractFilePath(Application.ExeName) + 'app.ini');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
//
Edit2.Text := IniFile.ReadString('APPLICATION','INI','');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
//
IniFile.Free;
end;

end.

Open in new window

I still don't understand what the is the purpose of all this.
I Find Systan courageous to try to sort it out.

Why link a file name to a form's caption ??? that is NOT a good idea. Use your form's NAME instead, if you don't want to use the EXE name.

As systan explained, you usually put your Ini file next to the exe that uses it. And it's a bad habit to put exe (and other big files) in your desktop anyway, just use shortcuts.

Whatever you wish to achieve, you can do it in one line code, maybe 2, with my unit.
If you have trouble to understand how it works, then please ask.
But since I DON'T UNDERSTAND what you want, I can't help you further.
Avatar of pr2501

ASKER

Form caption is a variable:

specific      Archive is located :  C:\specific
(attached picture)

And in this case i want to put ini into
C:\specific\specific.ini
I can get line above with:
edit1.Text:= (ExtractPath(form1.caption)+'\'+ExtractPathIni(form1.caption)+'.ini');
end;

Do  You understand me?
 
 

Function TForm1.ExtractPath(Str:String):String;
Var
 P:Integer;
begin
 P:=Pos(':\',Str);
 if P>0 Then Result:=Copy(Str,P-1,Length(Str)) Else Result:='';
end;

Function TForm1.ExtractPathIni(Str:String):String;
Var
 PI:Integer;
begin
 PI:=Pos(':\',Str);
 if PI>0 Then Result:=Copy(Str,PI+2,Length(Str)) Else Result:='';
end;

Open in new window

specific.JPG
Avatar of pr2501

ASKER

procedure TForm1.FormCreate(Sender: TObject);

with TINIFile.Create(ExtractPath(form1.caption)+'\'+ExtractPathIni(form1.caption)+'.ini') do

this can't work because form1 doesn't exist.

What can i do to resolve this problem?

Now i understand why You want to put ini file together with exe somewhere else from desktop , and to make shortcut for app.

I well think about accepting this resolution.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

thank you

Avatar of pr2501

ASKER


I houpe You will help me forward in my learning with Delphi and to akt more profesional.