Finding the path to a executable?

Ali Çehreli acehreli at yahoo.com
Tue Aug 6 23:10:16 PDT 2013


On 08/06/2013 10:31 PM, Alan wrote:
> Hello!  This may seem like a simple question, maybe an embarassing
> question but how would I fetch the absolute path to the directory where
> my executable is located?  My wording is known to be confusing so I will
> give an example:
>
> cd ~/projects/program
> dmd Program.d -ofProgram
>
> That executable would be located in /home/alan/projects/program for
> example.  SO my question is how would I fetch the absolute path to that
> directory?
>
> Thanks for any help!

First program argument is the absolute path to the executable. (I am not 
sure whether this is portable.)

import std.stdio;
import std.path;

void main(string[] args)
{
     writefln("I am program '%s' in '%s'.",
              baseName(args[0]), dirName(args[0]));
}

Ali



More information about the Digitalmars-d mailing list