简体   繁体   English

Dotnet Core - 获取应用程序的启动路径

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

相关问题 Rider 说无法更新路径“/usr/share/dotnet/dotnet”中的 dotnet 核心 - Rider saids can not update dotnet core in path “/usr/share/dotnet/dotnet” 在Linux上使用dotnet核心获取私钥 - get private key with dotnet core on linux 当我启动我的java应用程序时,我可以在我的linux控制台上获取路径吗? - When I launch my java application, can I get the path on my linux console? 为什么 RedHat dotnet 核心漏洞需要重建您的应用程序? - Why does the RedHat dotnet core vulnerability require rebuilding your application? 如何使用 Nginx 服务器在 Ubuntu 服务器上部署 dotnet 核心应用程序? - How to deploy dotnet core application on Ubuntu server with Nginx server? json反序列化导致dotnet核心应用程序中止 - 不会抛出任何异常 - json deserialization causes dotnet core application to abort - no exceptions are thrown 解决“终端进程启动失败:shell可执行文件“do.net”的路径不存在”问题的方法。 - A way to solve the problem "the terminal process failed to launch: path to shell executable "dotnet" does not exist." 试图让 Emgu.CV 在 debian 上的 dotnet core 3.1 中运行 - Trying to get Emgu.CV running in dotnet core 3.1 on debian Dotnet Core:使用 PDFium 将 Pdf 转换为 Linux 上的 Image 得到 DllNotFoundException - Dotnet Core: use PDFium to convert Pdf to Image on Linux get DllNotFoundException dotnet核心中的SocketCAN - SocketCAN in dotnet core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM