pr2501
asked on
Wor with ini file
With :
with TINIFile.Create(ExtractPat
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;
procedure TForm1.FormCreate(Sender: TObject);
begin
...
...
Form1.Caption := ExtractFilePath(Applicatio n.ExeName) ;
...
...
end;
begin
...
...
Form1.Caption := ExtractFilePath(Applicatio
...
...
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(ExtractFil
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;
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;
> is it posible to save into ini file name of app: myapp.exe ?
sure
IniFile.WriteString('APPLI CATION','N AME',Extra ctFileName (Applicati on.ExeName ));
IniFile.WriteString('APPLI CATION','P ATH',Extra ctFilePath (Applicati on.ExeName ));
but what is that useful for ? Is your Ini file used by more than one application ?
sure
IniFile.WriteString('APPLI
IniFile.WriteString('APPLI
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.
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.
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 )+'\'+Extr actPathIni (form1.cap tion)+'.in i');
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(ExtractPat h(form1.ca ption)+'\' +ExtractPa thIni(form 1.caption) +'.ini') do
caption:= ReadString(Self.Name, 'Caption', Caption);
edit1.text:= form1.ExtractPath(form1.ca ption);... .....
and if i look into ini file i can find ewerything it in?
C:\test2\test2.ini
with>
procedure TForm1.Button2Click(Sender
begin
edit1.Text:= (ExtractPath(form1.caption
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(ExtractPat
caption:= ReadString(Self.Name, 'Caption', Caption);
edit1.text:= form1.ExtractPath(form1.ca
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;
arhive.JPG
caption:= ReadString(Self.Name, 'Caption', Caption); ?
Why not;
Write;
IniFile.WriteString('APPLI CATION', 'Caption', ExtractFileName(Applicatio n.ExeName) );
Read;
caption := ReadString('APPLICATION', 'Caption', Caption); ?
And there is no, undeclared identifier.
form1.ExtractPath
Why not;
Write;
IniFile.WriteString('APPLI
Read;
caption := ReadString('APPLICATION', 'Caption', Caption); ?
And there is no, undeclared identifier.
form1.ExtractPath
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.
When i work with
...
with TINIFile.Create('C:\my.ini
....
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;
ASKER
But there must be somtheing that i'm missing, because when i reopen my forme it's empty.
ASKER
procedure TForm1.Button2Click(Sender
begin
edit1.Text:= (ExtractPath(form1.caption
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;
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.
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.
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.
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 )+'\'+Extr actPathIni (form1.cap tion)+'.in i');
end;
Do You understand me?
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
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;
specific.JPG
ASKER
procedure TForm1.FormCreate(Sender: TObject);
with TINIFile.Create(ExtractPat h(form1.ca ption)+'\' +ExtractPa thIni(form 1.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.
with TINIFile.Create(ExtractPat
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you
ASKER
I houpe You will help me forward in my learning with Delphi and to akt more profesional.
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(ExtractFil
...
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