[英]Dotnet Core - Get the application's launch path
Question - Is there a better/right way to get the application's launch path?问题 - 是否有更好/正确的方法来获取应用程序的启动路径?
Setup - I have a console application that runs in a Linux Debian docker image.设置 -我有一个在 Linux Debian docker 映像中运行的控制台应用程序。 I am building the application using the --runtime linux-x64
command line switch and have all the runtime identifiers set appropriately.我正在使用--runtime linux-x64
命令行开关构建应用程序,并正确设置了所有运行时标识符。 I was expecting the application to behave the same whether launching it by calling dotnet MyApplication.dll
or ./MyApplication
but they are not.无论是通过调用dotnet MyApplication.dll
还是./MyApplication
来启动它,我都希望应用程序的行为相同,但事实并非如此。
Culprit Code - I have deployed files in a folder below the application directory that I reference so I do the following to get what I consider my launch path.罪魁祸首代码 -我已将文件部署在我引用的应用程序目录下方的文件夹中,因此我执行以下操作以获取我认为的启动路径。 I have read various articles saying this is the correct way to get what I want, and it works depending on how I launch it.我读过各种文章,说这是获得我想要的东西的正确方法,它的工作原理取决于我如何启动它。
using var processModule = Process.GetCurrentProcess().MainModule;
var basePath = Path.GetDirectoryName(processModule?.FileName);
When launching this using the comand dotnet MyApplication.dll
the above codes path is /usr/share/dotnet
使用命令dotnet MyApplication.dll
启动此代码时,上述代码路径为/usr/share/dotnet
When launching this using the command ./MyApplication.dll
the path is then /app
使用命令./MyApplication.dll
启动此路径时,路径为/app
I understand why using dotnet
would be different as it is the process that is running my code, but again it was unexpected.我理解为什么使用dotnet
会有所不同,因为它是运行我的代码的进程,但这又是出乎意料的。
Any help here to what I should use given the current environment would be appreciated.在给定当前环境的情况下,我应该使用的任何帮助将不胜感激。 Ultimately I need the path where the console application started from as gathered by the application when it starts up.最终,我需要应用程序启动时收集的控制台应用程序启动的路径。
Thanks for your help.谢谢你的帮助。
This code should work:此代码应该可以工作:
public static IConfiguration LoadConfiguration()
{
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
.....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.