Get path of executable
There is no cross platform way that I know.
For Linux: pass "/proc/self/exe"
to std::filesystem::canonical
or readlink
.
Windows: pass NULL as the module handle to GetModuleFileName
.
Get executable's path (with std::filesystem) [duplicate]
No, there's nothing provided in the standard filesystem facilities to get the path of your executable.
Even using using the 1st argv
argument isn't guaranteed to contain the full path of the executable.
The systems I know will just pass in the string that was used to launch the program.
Considering that this could be resolved using the PATH
environment variable, there's no guarantee, you see a full path there.
There are some OS specific methods to do so though:
- Get path of executable
get path for my .exe [duplicate]
System.Reflection.Assembly.GetEntryAssembly().Location;
Getting the absolute path of the executable, using C#?
MSDN has an article that says to use System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
; if you need the directory, use System.IO.Path.GetDirectoryName
on that result.
Or, there's the shorter Application.ExecutablePath
which "Gets the path for the executable file that started the application, including the executable name" so that might mean it's slightly less reliable depending on how the application was launched.
Getting the path of the executable file's current location
It isn't hard.
But it MIGHT depend on your target platform (which you haven't specified).
For .Net Core 1.x and .Net 5 or higher, I would use AppContext.BaseDirectory
Here are some other alternatives for various environments over the years:
6 ways to get the current directory in C#, August 17,
2010
AppDomain.CurrentDomain.BaseDirectory
This is the best option all round. It will give you the base directory for class libraries,
including those in ASP.NET applications.Directory.GetCurrentDirectory()
Note: in .NET Core this is the current best practice. The details below relate to the .NET Framework
4.5 and below.This does an interop call using the winapi GetCurrentDirectory call
inside kernel32.dll, which means the launching process’ folder will
often be returned. Also as the MSDN documents say, it’s not guaranteed
to work on mobile devices.Environment.CurrentDirectory
This simply calls Directory.GetCurrentDirectory()Assembly.Location
This would be called using
this.GetType().Assembly.Location
This returns the full path to the calling assembly, including the
assembly name itself. If you are calling a separate class library,
then its base directory will be returned, such “C:\myassembly.dll” -
depending obviously on which Assembly instance is being used.Application.StartupPath
This is inside the System.Windows.Forms namespace, so is typically used in window forms application only.Application.ExecutablePath
The same as Application.StartupPath, however this also includes the application name, such as “myapp.exe”
C++ [Windows] Path to the folder where the executable is located [duplicate]
Use GetModuleFileName to find out where your exe is running from.
WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
Then strip the exe name from path.
How to get the full path of an executable in $PATH in Go
Use os/exec.LookPath:
LookPath searches for an executable named file in the directories named by the PATH environment variable. If file contains a slash, it is tried directly and the PATH is not consulted. The result may be an absolute path or a path relative to the current directory.
Use path/filepath.Abs if you need the path to be absolute in all cases:
package main
import (
"log"
"os/exec"
"path/filepath"
)
func main() {
fname, err := exec.LookPath("go")
if err == nil {
fname, err = filepath.Abs(fname)
}
if err != nil {
log.Fatal(err)
}
log.Println(fname)
}
Find the path to the executable
Use package osext.
It's providing function Executable()
that returns an absolute path to the current program executable.
It's portable between systems.
Online documentation
package main
import (
"github.com/kardianos/osext"
"fmt"
)
func main() {
filename, _ := osext.Executable()
fmt.Println(filename)
}
Python get path to executable file?
I think, os.getcwd()
and sys.executable
is what you are looking for.
With these 2 functions you will know the folder you'r script running in (from where you started it), and the path to a current python executable (which probably will be included in a .exe distribution, and unpacked to a tmp dir):
$> mkdir -p /tmp/x
$> cd /tmp/x
$> pwd
/tmp/x
$> which python
/usr/bin/python
$> python
Python 3.9.5 (default, May 24 2021, 12:50:35)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
'/tmp/x'
>>> import sys
>>> sys.executable
'/usr/bin/python'
>>>
how to get path to executable in C running on Windows?
Use GetModuleFileName() and pass NULL as the first argument:
DWORD last_error;
DWORD result;
DWORD path_size = 1024;
char* path = malloc(1024);
for (;;)
{
memset(path, 0, path_size);
result = GetModuleFileName(0, path, path_size - 1);
last_error = GetLastError();
if (0 == result)
{
free(path);
path = 0;
break;
}
else if (result == path_size - 1)
{
free(path);
/* May need to also check for ERROR_SUCCESS here if XP/2K */
if (ERROR_INSUFFICIENT_BUFFER != last_error)
{
path = 0;
break;
}
path_size = path_size * 2;
path = malloc(path_size);
}
else
{
break;
}
}
if (!path)
{
fprintf(stderr, "Failure: %d\n", last_error);
}
else
{
printf("path=%s\n", path);
}
Related Topics
Why Are C Character Literals Ints Instead of Chars
What Are Some Uses of Template Template Parameters
How to Print a List of Elements Separated by Commas
Opencv C++/Obj-C: Detecting a Sheet of Paper/Square Detection
Utf8 To/From Wide Char Conversion in Stl
Why Is Enum Class Preferred Over Plain Enum
Reading from Text File Until Eof Repeats Last Line
Reflection and Refraction Impossible Without Recursive Ray Tracing
How to Concatenate a Std::String and an Int
What Does T&& (Double Ampersand) Mean in C++11
Usr/Bin/Ld: Cannot Find -L≪Nameofthelibrary≫
What Is a "Span" and When Should I Use One
Delete VS Delete[] Operators in C++
What Exactly Is One Definition Rule in C++
Using Generic Std::Function Objects With Member Functions in One Class