Recent

Author Topic: How to get application path on Linux  (Read 21277 times)

zsver

  • Newbie
  • Posts: 6
How to get application path on Linux
« on: March 23, 2008, 07:19:07 pm »
Hello,

Somebody has an idea, how to get the path of the application under linux?
like in windows : ExtractFilePath(Application.EXEName)

after some googling I find the following site.
http://wiki.freepascal.org/Multiplatform_Programming_Guide

according to the sample code on the page, I should use GetAppConfigDir(False) to get the application path , but it is not give the correct result.

linux is Ubuntu 7.10 (Gutsy)

Thanks
Zsolt

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
RE: How to get application path on Linux
« Reply #1 on: March 23, 2008, 07:57:03 pm »
AFAIK, on linux isn't [always] precise the app directory (mainly if program is execute on terminal)...
I believe that the better thing to do is make a default dir to your application (something as /usr/local/your_app...

But, maybe I'm wrong =)

Regards,
Júnior
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: How to get application path on Linux
« Reply #2 on: March 23, 2008, 08:11:40 pm »
Quote from: "zsver"
Somebody has an idea, how to get the path of the application under linux?
like in windows : ExtractFilePath(Application.EXEName)

after some googling I find the following site.
http://wiki.freepascal.org/Multiplatform_Programming_Guide

according to the sample code on the page, I should use GetAppConfigDir(False) to get the application path , but it is not give the correct result.


On Linux and OS X, ParamStr(0) and the like only return the path used to start the program. For example, if you started a program from the command line like this:

./myapp

then ParamStr(0) returns "./myapp", not the expanded path we're used to on Windows.

However, you might try using ExpandFileName and see if that doesn't return the expanded path to the executable:

  WriteLn(ExpandFileName(ParamStr(0)));

GetAppConfigDir and GetAppConfigFile return what FPC thinks are good places to store configuration data, not the app's path. I don't use these functions since they return nonsensical results on OS X.

Thanks.

-Phil

andrew.higgs

  • New Member
  • *
  • Posts: 22
Re: How to get application path on Linux
« Reply #3 on: April 10, 2008, 09:09:12 am »
Quote from: "zsver"
Hello,

Somebody has an idea, how to get the path of the application under linux?
like in windows : ExtractFilePath(Application.EXEName)

after some googling I find the following site.
http://wiki.freepascal.org/Multiplatform_Programming_Guide

according to the sample code on the page, I should use GetAppConfigDir(False) to get the application path , but it is not give the correct result.

linux is Ubuntu 7.10 (Gutsy)

Thanks
Zsolt


ExtractFilePath(Application.Exename) works on Linux and windows for me.

Regards
Andrew

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
RE: Re: How to get application path on Linux
« Reply #4 on: April 11, 2008, 03:23:54 am »
I think it is
  Application.Location

cyber_python

  • Jr. Member
  • **
  • Posts: 79
RE: Re: How to get application path on Linux
« Reply #5 on: April 11, 2008, 01:30:57 pm »
In general, when deploying an application for Linux systems it will be installed in /usr/share/bin or /usr/share/local/bin. So, if you want the app dir to store and read files for configuration/options etc. /usr/... won't be very useful. I suggest you use a folder in the user's HOME directory, for example a directory named ".appname" . You can get it using the following code:

Code: [Select]

function TForm1.getHomeFolder : String;
var
 envlist : TStringList;
 i : integer;
 found : boolean;
home_folder : String;
begin
  envList:=TStringlist.Create;
  application.GetEnvironmentList(envList);
  i:=0;
  found:=false;
  while( (i<envList.Count) AND (not found)  )do
  begin
    if(AnsiContainsStr(envList.strings[i],'HOME='))then
        begin
          found := true;
        end;
    i:=i+1;
  end;
 
  if(found)then
  begin
      home_folder := copy(envList.Strings[i-1],6, length(envList.Strings[i-1])-5);
      //showmessage('home_folder set to : '+home_folder);
  end
  else
  begin
    home_folder := '';
    messageDlg('Error : Could not determine your home folder!',mtError,[mbOK],0);
    selectDirectory('Choose your home folder', '/home', home_folder);
  end;
  //showmessage(target_folder);
  envList.Free;
result:=home_folder;
end;

DonaldShimoda

  • Full Member
  • ***
  • Posts: 109
    • Parallel Pascal Worlds
Re: How to get application path on Linux
« Reply #6 on: November 22, 2012, 05:29:06 pm »

ExtractFilePath(Application.Exename) works on Linux and windows for me.


Can you please click on Application and tell us in what units is present?

Best regards!

SunyD

  • Guest
Re: How to get application path on Linux
« Reply #7 on: November 22, 2012, 05:51:13 pm »
You don't need additional unit for this and works on Windows too:
Code: [Select]
ExtractFilePath(ParamStr(0)); 

DonaldShimoda

  • Full Member
  • ***
  • Posts: 109
    • Parallel Pascal Worlds
Re: How to get application path on Linux
« Reply #8 on: November 22, 2012, 06:02:59 pm »
You don't need additional unit for this and works on Windows too:
Code: [Select]
ExtractFilePath(ParamStr(0)); 

But in this thread somebody says it dont always work as expected... %)

DonaldShimoda

  • Full Member
  • ***
  • Posts: 109
    • Parallel Pascal Worlds
Re: How to get application path on Linux
« Reply #9 on: November 22, 2012, 06:17:31 pm »

ExtractFilePath(Application.Exename) works on Linux and windows for me.


Dor the record, was my mistake, unit Forms make the trick.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How to get application path on Linux
« Reply #10 on: November 22, 2012, 06:35:13 pm »

ExtractFilePath(Application.Exename) works on Linux and windows for me.

Afaik it doesn't work on Darwin, but it works on Linux and recently (since 2.6.0) on FreeBSD.

It is not guaranteed to work on Linux and FreeBSD, since afaik there it is dependent on the filesystem, and not every (virtual/network!-) filesystem might support it.

But currently I'm not aware of cases where it doesn't work. If somebody does, please notify us and we'll document it.

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: How to get application path on Linux
« Reply #11 on: November 27, 2012, 01:11:29 pm »
Somebody has an idea, how to get the path of the application under linux?

Have a look for "function ProgramDirectory:string;" in unit "FileUtils;".
Lazarus 1.7 (SVN) FPC 3.0.0

ArminLinder

  • Sr. Member
  • ****
  • Posts: 314
  • Keep it simple.
Re: How to get application path on Linux
« Reply #12 on: July 23, 2021, 03:53:54 pm »
Just for completeness, and welcome in 2021 where, after 10 years of progress in IT, still the same problems exist ...

Ocye, your tip is the only one which works with Windows and macOS. Just note that the name is not FileUtils, but FileUtil :-)

Under Windows, more or less all the commands mentioned will give either the directory of the current program, or the full path of the exe, from where the directory name van easily be extracted.

Quote
23.07.2021 15:40:04: ProgramDirectory: D:\Users\Linder\Documents\Programmierung\lazarus\Components\GenericFileDatabase\
23.07.2021 15:40:04: ExeName: D:\Users\Linder\Documents\Programmierung\lazarus\Components\GenericFileDatabase\project1.exe
23.07.2021 15:40:04: ParamStr(0): D:\Users\Linder\Documents\Programmierung\lazarus\Components\GenericFileDatabase\project1.exe
23.07.2021 15:40:04: PWD:
23.07.2021 15:40:04: GetCurrentDir: D:\Users\Linder\Documents\Programmierung\lazarus\Components\GenericFileDatabase

On the Mac (Catalina) they all do not return what's expected, even the query of the PWD variable fails for unknown reasons (it does exist in the terminal) , except yours:

Quote
23-7-21 15:09:31: ProgramDirectory: /Users/linder/Documents/Programming/Lazarus/Components/GenericFileDatabase/
23-7-21 15:09:31: ExeName: /Users/linder/Documents/Programming/Lazarus/Components/GenericFileDatabase/project1.app/Contents/MacOS/project1
23-7-21 15:09:31: ParamStr(0): /Users/linder/Documents/Programming/Lazarus/Components/GenericFileDatabase/project1.app/Contents/MacOS/project1
23-7-21 15:09:31: PWD:
23-7-21 15:09:31: GetCurrentDir: /

Unfortunately I have no Linux at hand to check.

Thumbs up!

Armin.


Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to get application path on Linux
« Reply #13 on: July 23, 2021, 05:29:25 pm »
Hi!


The linux implementation of application.exename does exactly what was mentioned above in unit custApp:


Code: Pascal  [Select][+][-]
  1. Result:=Paramstr(0);

Winni

 

TinyPortal © 2005-2018