Logo

Programming-Idioms

Assign to string dir the path of the folder containing the currently running executable.
(This is not necessarily the working directory, though.)
Implementation
C

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another C implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import "os"
import "path/filepath"
programPath := os.Args[0]
absolutePath, err := filepath.Abs(programPath)
if err != nil {
	return err
}
dir := filepath.Dir(absolutePath)
import std.path;
void main(string[] args) {
    string dir = args[0].dirName;
}
dir := extractfilepath(paramstr(0));
$dir = realpath(dirname(__FILE__));
dir = __dir__
import System.Environment (getExecutablePath)
import System.FilePath.Windows
dir <- takeDirectory `fmap` getExecutablePath
import System.Environment (getExecutablePath)
import System.FilePath.Windows
import System.IO.Unsafe (unsafePerformIO)
dir = unsafePerformIO (takeDirectory `fmap` getExecutablePath)
import os
dir = os.path.dirname(os.path.abspath(__file__))
const path = require('path');
const dir = path.resolve();
use English qw($EXECUTABLE_NAME);
use Path::Tiny qw(path);
my $dir = path($EXECUTABLE_NAME)->parent;
dir = AppDomain.CurrentDomain.BaseDirectory;
dir = arg[1]
let dir = std::env::current_exe()?
    .canonicalize()
    .expect("the current exe should exist")
    .parent()
    .expect("the current exe should be a file")
    .to_string_lossy()
    .to_owned();
const dir = __dirname;
uses SysUtils;
dir := GetCurrentDir;
from pathlib import Path
dir = str(Path(__file__).parent)