www.digitalmars.com         C & C++   DMDScript  

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

reply "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
next sibling parent reply "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
parent reply 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
parent "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
prev sibling next sibling parent reply 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
parent reply "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 
 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!
Feb 18 2013
parent reply 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
parent 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
prev sibling next sibling parent "shuji" <cravstar hotmail.com> writes:
There is a solution already for this:



(just for future reference, it's seriously hard to search on 
google)
Jul 31 2014
prev sibling parent "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