E
E
Evgeny Ivanov2020-03-27 14:19:22
Delphi
Evgeny Ivanov, 2020-03-27 14:19:22

How to open a file with your program (associate it with the program)?

I have a file that says the following

<PROJECT_SETTINGS>
<FOLDER_PATH>D:\test</FOLDER_PATH>
<ZIP_FILE_PATH>D:\arhive_test</ZIP_FILE_PATH>
</PROJECT_SETTINGS>

Here is such an xml file with project settings.

The program has a procedure
//Загрузить все настройки проекта из файла
procedure read_project_from_file(file_path:string);
var
current_string:string;
text_file:TextFile;
income_tags:boolean;
begin
AssignFile (text_file, file_path);
Reset (text_file);
while not EOF(text_file) do
begin
readln(text_file, current_string);

if (CountPos('<PROJECT_SETTINGS>',current_string)>0) then income_tags:=true;
if (CountPos('</PROJECT_SETTINGS>',current_string)>0) then income_tags:=false;

if (income_tags=true) then
begin
if (CountPos('<FOLDER_PATH>',current_string)>0) then folder_path:=get_stext('<FOLDER_PATH>','</FOLDER_PATH>',current_string);
if (CountPos('<ZIP_FILE_PATH>',current_string)>0) then zip_file_folder:=get_stext('<ZIP_FILE_PATH>','</ZIP_FILE_PATH>',current_string);
end;

//Заполняем форму
if (income_tags=false) then
begin
Form1.folder_path_directory_edit.Text:=folder_path;
Form1.zip_file_path_directory_edit.Text:=zip_file_folder;
end;
end;
CloseFile (text_file);
end;

The procedure reads the file line by line, finds tags there, for example FOLDER_PATH and copies everything between them into variables. And then from the variables to the "text-edit" on the form.
And here "text-edit" contains the path to the folder.

The procedure is called on the click of a button.
procedure TForm1.open_project_buttonClick(Sender: TObject);
begin
if not OpenDialog1.Execute then Exit;
read_project_from_file(OpenDialog1.FileName);
end;

I pressed the button, a dialog opened, indicated the file, the procedure read lines from it and substituted their contents into the fields. Everything is simple.

Now I would like to open this text xml file not only from the program, but also by double clicking.
How can I do that?

Of course, the file has its own extension .my_xml. Associating it with the program is also quite simple.
But what to do next? How do you "tell" a program that when you double-click on a file, it should run and perform a certain procedure?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hemul GM, 2020-03-27
@logpol32

Google: "Delphi file association"
The first point answers your question.

Z
zed, 2020-03-27
@zedxxx

When you have associated a file extension with a program, Windows will run your program by passing the name of the clicked file as command line options. Thus, when starting the application, you need to check for the presence of this parameter, and if it is, then call the file opening procedure.
The only thing to keep in mind is that the first parameter is always the name of your program, so look at the second parameter.
To access the parameters, there is the System.ParamStr function .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question