简体   繁体   English

获取自带EXE的路径?

[英]Get self-contained EXE's path?

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?如果我们正在部署单个文件应用程序,有没有办法从我们的代码中获取单个文件应用程序.exe的路径?

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.我问的原因是因为我有一个示例控制台应用程序,我想在注册表中注册以在每次 windows 启动时启动,但遗憾的是每个已知的 API 像Assembly.GetExecutingAssembly().Location或建议的通过 Microsoft AppContext.BaseDirectory返回相对于控制台应用程序的实际.dll文件的路径,而不是解包它的自包含.exe

From GitHub issue , suggestion by @Symbai:来自GitHub 问题,@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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 net5 自包含的 exe 缺少一些系统 DLL - net5 self-contained exe misses some system DLLs 独立的普通纪念品 - Self-contained generic memento 如何检查我的 .NET Core 控制台应用程序是作为独立应用程序运行(通过运行 myapp.exe)还是使用 dotnet myapp.dll 运行? - How to check if my .NET Core console app is running as a self-contained app (by running myapp.exe) or if it's running using dotnet myapp.dll? 引用的项目是一个非独立的可执行文件。 自包含的可执行文件不能引用非自包含的可执行文件 - The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable 例如 C# 自包含 getter - C# Self-contained getter for instance 用于 Selenium 测试的独立镀铬 - Self-Contained Chrome for Selenium Testing 当一个独立的 .NetCore 应用程序正在运行时,启动另一个共享相同运行时的 .NetCore 应用程序的最佳方式是什么? - While a self-contained .NetCore app is running, what's the best way to start another .NetCore app sharing the same runtime? 是否可以将MsTest项目构建为独立的可执行文件? 怎么样? - Is it possible to build an MsTest project into a self-contained executable? How? 检测是否从已发布的自包含可执行文件运行 - Detect if running from published self-contained executable 单元测试:自包含测试与代码重复(DRY) - Unit Testing: Self-contained tests vs code duplication (DRY)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM