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.

Reply via email to