Thread overview
How to detect current executable file name?
Feb 18, 2013
eGust
Feb 18, 2013
SaltySugar
Feb 18, 2013
Jonathan M Davis
Feb 18, 2013
eGust
Feb 18, 2013
Jacob Carlborg
Feb 18, 2013
eGust
Feb 18, 2013
Jacob Carlborg
Feb 19, 2013
Marco Leise
Jul 31, 2014
shuji
Aug 01, 2014
Gary Willoughby
February 18, 2013
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.
February 18, 2013
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]);
}
February 18, 2013
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
February 18, 2013
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
February 18, 2013
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.
February 18, 2013
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 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

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!
February 18, 2013
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
February 19, 2013
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 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/… 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.

-- 
Marco

July 31, 2014
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 google)
August 01, 2014
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()));
}