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 [email protected] 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

To get full path of powershell.exe programmatically

From Dev

Windows: get the path of an exe by application name or identifier?

From Dev

Regex for contained path

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

How do I get Chocolatey to not add an EXE to the path?

From Dev

How to get the path of the installed Excel.exe programmatically?

From Dev

How to get path to firefox.exe and equiv in other os

From Dev

How to get the path of the installed Excel.exe programmatically?

From Dev

How to get simulator(.exe) path from dll in C

From Dev

How to get the path for .exe file which resides in src/main/resources

From Dev

Get class contained in RLMArray

From Dev

How to add batch commands to cmd.exe's PATH

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

Related Related

  1. 1

    Pass self reference to contained object's function

  2. 2

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

  3. 3

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

  4. 4

    Self Contained WPF .net

  5. 5

    To get full path of powershell.exe programmatically

  6. 6

    Windows: get the path of an exe by application name or identifier?

  7. 7

    Regex for contained path

  8. 8

    Self-contained generic memento

  9. 9

    Self-contained shared library

  10. 10

    Self-Contained Linked List

  11. 11

    Snappy and self-contained programs

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    How do I get Chocolatey to not add an EXE to the path?

  16. 16

    How to get the path of the installed Excel.exe programmatically?

  17. 17

    How to get path to firefox.exe and equiv in other os

  18. 18

    How to get the path of the installed Excel.exe programmatically?

  19. 19

    How to get simulator(.exe) path from dll in C

  20. 20

    How to get the path for .exe file which resides in src/main/resources

  21. 21

    Get class contained in RLMArray

  22. 22

    How to add batch commands to cmd.exe's PATH

  23. 23

    A web crawler in a self-contained python file

  24. 24

    Ensuring that a maven build is self-contained

  25. 25

    Fully self-contained HTML files with Pandoc

  26. 26

    completely self-contained virtual environment

  27. 27

    Is a python virtual environment entirely self-contained?

  28. 28

    AngularJS self contained directives for designer use

  29. 29

    Deploying self-contained native OCaml application

HotTag

Archive