[英]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.