Get self-contained EXE's path?

SpiritBob

If we're deploying a single file application, is there a way to get the single file application .exe's path from inside our code?

The reason I'm asking is because I have a sample console application, which I'd like to register in the registry to start every time windows starts, but sadly every known API like Assembly.GetExecutingAssembly().Location, or the one suggested by Microsoft AppContext.BaseDirectory return paths relative to the actual .dll file of the console application, not the self-contained .exe which unpackages it all.

Alex Riveron

From GitHub issue, suggestion by @Symbai:

public static class Extensions {
    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    static extern uint GetModuleFileName(IntPtr hModule, System.Text.StringBuilder lpFilename, int nSize);
    static readonly int MAX_PATH = 255;
    public static string GetExecutablePath() {
        if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) {
            var sb = new System.Text.StringBuilder(MAX_PATH);
            GetModuleFileName(IntPtr.Zero, sb, MAX_PATH);
            return sb.ToString();
        }
        else {
            return System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        }
    }
}

Collected from the Internet

Please contact debugcn@gmail.com to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass self reference to contained object's function

From Dev

How to get the path of a folder that's compiled into a python exe?

From Dev

WPF .NET Core 3.1 published self contained single exe missing DLL

From Dev

Self Contained WPF .net

From Dev

Regex for contained path

From Dev

To get full path of powershell.exe programmatically

From Dev

Self-contained generic memento

From Dev

Self-contained shared library

From Dev

Self-Contained Linked List

From Dev

Snappy and self-contained programs

From Dev

get string from list if it's contained in another string column

From Dev

Get the latest version of software from PowerShell and get the respective exe path

From Dev

How to get all points contained within an SVG path string without checking every point on canvas?

From Dev

Get class contained in RLMArray

From Dev

A web crawler in a self-contained python file

From Dev

Ensuring that a maven build is self-contained

From Dev

Fully self-contained HTML files with Pandoc

From Dev

completely self-contained virtual environment

From Java

Is a python virtual environment entirely self-contained?

From Dev

AngularJS self contained directives for designer use

From Dev

Deploying self-contained native OCaml application

From Dev

self-contained/serverless nosql databases

From Dev

How to Make RStudio Presentation Self-contained?

From Dev

Include icon in Self-Contained JavaFX application

From Dev

Self contained login using HTML and/or Javascript

From Dev

Self-Contained Applications, built in Java

From Dev

A self-contained R in OS X

From Dev

Backup mysql databases into self contained files

From Dev

Does dlopen require a lib to be self-contained?

Related Related

HotTag

Archive