## digitalmars.D.learn - How to detect current executable file name?

• eGust (6/6) Feb 17 2013 I need to locate the directory of current executable file, but I
• SaltySugar (6/12) Feb 17 2013 import std.stdio;
• Jonathan M Davis (9/23) Feb 17 2013 That'll tell you the name of the executable, but the path is relative to...
• eGust (9/40) Feb 17 2013 This way won't work. main's args[0] (which also is
• Jacob Carlborg (4/9) Feb 17 2013 http://www.dsource.org/projects/tango/attachment/ticket/1536/process.d
• eGust (14/26) Feb 18 2013 Thanks for your reply. Does anyone know, will it be added into
• Jacob Carlborg (6/18) Feb 18 2013 Yeah, GetModuleFileNameW should be used. I'm pretty sure the other
• Marco Leise (13/15) Feb 18 2013 I also found that console output on Windows works best with
• shuji (4/4) Jul 31 2014 There is a solution already for this:
• Gary Willoughby (8/14) Aug 01 2014 import std.stdio;
"eGust" <egustc gmail.com> writes:
I need to locate the directory of current executable file, but I
can't find how to do that in Phobos. I tried
core.runtime.Runtime.args[0], but failed. Is there a standard
method of Phobos to do that? I only know the way of Windows
(GetModuleFileName), but I think as a common task there should be
a platform-independent way to get it in the standard library.

Feb 17 2013
"SaltySugar" <Saltysugar inbox.lt> writes:
On Monday, 18 February 2013 at 03:28:59 UTC, eGust wrote:
I need to locate the directory of current executable file, but
I can't find how to do that in Phobos. I tried
core.runtime.Runtime.args[0], but failed. Is there a standard
method of Phobos to do that? I only know the way of Windows
(GetModuleFileName), but I think as a common task there should
be a platform-independent way to get it in the standard library.

import std.stdio;

void main (string[] args)
{
writeln(args[0]);
}

Feb 17 2013
Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, February 18, 2013 07:59:08 SaltySugar wrote:
On Monday, 18 February 2013 at 03:28:59 UTC, eGust wrote:
I need to locate the directory of current executable file, but
I can't find how to do that in Phobos. I tried
core.runtime.Runtime.args[0], but failed. Is there a standard
method of Phobos to do that? I only know the way of Windows
(GetModuleFileName), but I think as a common task there should
be a platform-independent way to get it in the standard library.

import std.stdio;

void main (string[] args)
{
writeln(args[0]);
}

That'll tell you the name of the executable, but the path is relative to the
directory that the program was run from. However, if you combine that with
std.path.absolutePath, you should be able to get the absolute path to the
executable, and from that you should be able to get the directory with
std.path.dirName.

Alternatively, if you want to know the current working directory, then use
std.file.getcwd.

- Jonathan M Davis

Feb 17 2013
"eGust" <egustc gmail.com> writes:
On Monday, 18 February 2013 at 07:07:42 UTC, Jonathan M Davis
wrote:
On Monday, February 18, 2013 07:59:08 SaltySugar wrote:
On Monday, 18 February 2013 at 03:28:59 UTC, eGust wrote:
I need to locate the directory of current executable file,
but
I can't find how to do that in Phobos. I tried
core.runtime.Runtime.args[0], but failed. Is there a standard
method of Phobos to do that? I only know the way of Windows
(GetModuleFileName), but I think as a common task there
should
be a platform-independent way to get it in the standard
library.

import std.stdio;

void main (string[] args)
{
writeln(args[0]);
}

That'll tell you the name of the executable, but the path is
relative to the
directory that the program was run from. However, if you
combine that with
std.path.absolutePath, you should be able to get the absolute
path to the
executable, and from that you should be able to get the
directory with
std.path.dirName.

Alternatively, if you want to know the current working
directory, then use
std.file.getcwd.

- Jonathan M Davis

This way won't work. main's args[0] (which also is
Runtime.args[0]) only tells you the executed command line.
Suppose on Windows there is an exe file(foo.EXE) in a
dir(C:\bar), which is in %PATH%. Call "foo" anywhere(CWD==D:\),
then C:\bar\foo.EXE will be executed, and its args[0] will be
just "foo". Because absolutePath(args[0]) uses getcwd by default,
so "D:\foo"(getcwd~args[0]) will be incorrectly got.

Feb 17 2013
Jacob Carlborg <doob me.com> writes:
On 2013-02-18 04:28, eGust wrote:
I need to locate the directory of current executable file, but I can't
find how to do that in Phobos. I tried core.runtime.Runtime.args[0], but
failed. Is there a standard method of Phobos to do that? I only know the
way of Windows (GetModuleFileName), but I think as a common task there
should be a platform-independent way to get it in the standard library.

http://www.dsource.org/projects/tango/attachment/ticket/1536/process.d

--
/Jacob Carlborg

Feb 17 2013
"eGust" <egustc gmail.com> writes:
On Monday, 18 February 2013 at 07:32:06 UTC, Jacob Carlborg wrote:
On 2013-02-18 04:28, eGust wrote:
I need to locate the directory of current executable file, but
I can't
find how to do that in Phobos. I tried
core.runtime.Runtime.args[0], but
failed. Is there a standard method of Phobos to do that? I
only know the
way of Windows (GetModuleFileName), but I think as a common
should be a platform-independent way to get it in the standard
library.

http://www.dsource.org/projects/tango/attachment/ticket/1536/process.d

Thanks for your reply. Does anyone know, will it be added into
D2's standard library?

p.s. About the code, I don't know how it work on other platform,
but on windows it will get a bad result if the path contains any
non-ASCII character and D1's char[] is UTF-8 encoded.
GetModuleFileNameA will get a string encoded as the ACP setting
but not UTF-8. ASCII character is well compatible with most
character sets (Windows' ACP setting) including UTF-8. But once a
non-ASCII character in the path (like me, a Chinese user, a lot
Chinese characters namd dirs on my PC, ACP is 936--GBK), it
fails. Better way is to use GetModuleFileNameW and wchar[] then
convert back to char[].
Anyway, thank you very much!

Feb 18 2013
Jacob Carlborg <doob me.com> writes:
On 2013-02-18 09:21, eGust wrote:

Thanks for your reply. Does anyone know, will it be added into D2's
standard library?

Not until someone makes a pull request.

p.s. About the code, I don't know how it work on other platform, but on
windows it will get a bad result if the path contains any non-ASCII
character and D1's char[] is UTF-8 encoded. GetModuleFileNameA will get
a string encoded as the ACP setting but not UTF-8. ASCII character is
well compatible with most character sets (Windows' ACP setting)
including UTF-8. But once a non-ASCII character in the path (like me, a
Chinese user, a lot Chinese characters namd dirs on my PC, ACP is
936--GBK), it fails. Better way is to use GetModuleFileNameW and wchar[]
then convert back to char[].
Anyway, thank you very much!

Yeah, GetModuleFileNameW should be used. I'm pretty sure the other
platforms will return UTF-8.

--
/Jacob Carlborg

Feb 18 2013
Marco Leise <Marco.Leise gmx.de> writes:
Am Mon, 18 Feb 2013 10:07:45 +0100
schrieb Jacob Carlborg <doob me.com>:

Yeah, GetModuleFileNameW should be used. I'm pretty sure the other=20
platforms will return UTF-8.

I also found that console output on Windows works best with
wchars.
Linux doesn't have any official support to get the module file
name, except for "readlink /proc/<pid>/exe". Maybe because you
are not supposed to use e.g. /usr/local/=E2=80=A6 or ~/.<program>/
for data or because file paths don't mean that much when its
technically possible to move, hardlink or delete names of a
running program (unlike on Windows).
Despite that I find such a function useful in Phobos.

--=20
Marco

Feb 18 2013
"shuji" <cravstar hotmail.com> writes:
There is a solution already for this:

http://dlang.org/phobos/std_file.html#.thisExePath

(just for future reference, it's seriously hard to search on

Jul 31 2014
"Gary Willoughby" <dev nomad.so> writes:
On Monday, 18 February 2013 at 03:28:59 UTC, eGust wrote:
I need to locate the directory of current executable file, but
I can't find how to do that in Phobos. I tried
core.runtime.Runtime.args[0], but failed. Is there a standard
method of Phobos to do that? I only know the way of Windows
(GetModuleFileName), but I think as a common task there should
be a platform-independent way to get it in the standard library.

import std.stdio;
import std.file : thisExePath;
import std.path : dirName;

void main(string[] args)
{
writeln(dirName(thisExePath()));
}

Aug 01 2014