Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get actual path to executable when using .netcore 3.0 and using the /p:PublishSingleFile=true flag?

I recently upgraded an application to dotnet core 3 and started using the PublishSingleFile flag during the build process. With these two changes the way the executable path is found has changed. Now instead of getting the path where the executable file is, I get redirected to a random directory in /var/tmp/.net/ where as I used to get /opt/appdir/.

Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

Edit: So it seems like the random dir it is spitting out actually points to an unpacked version of my application which does not contain vital resources that are in the other directory.

like image 291
BillHaggerty Avatar asked Sep 24 '19 15:09

BillHaggerty


People also ask

How do I get the root path in .NET core?

The Application Base (Root) path is available in the ContentRootPath property of the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core. The IHostingEnvironment is an interface for .

How do I create a true Publishinginglefile?

Add <PublishSingleFile>true</PublishSingleFile> to your project file. On the Solution Explorer pane, right-click on the project you want to publish. Select Publish. If you don't already have a publishing profile, follow the instructions to create one and choose the Folder target-type.


2 Answers

The following seems to give the path you're after, to the original executable:

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
like image 143
Vimes Avatar answered Oct 13 '22 10:10

Vimes


You were getting the actual executable path. You just didn't understand what was going on. Using the PublishSingleFile is not generating a single file that when executes does exactly what the software is programmed to do. What is actually happening is that a publish with this flag generates a file that contains a packaged version of all your files, which when executed unpacks them into the a subdirectory of /var/tmp/.net and then executes it. So if you are moving configs into the publish dir after the publish process, they will not automatically make it to the executable location when you deploy and try to run it.

like image 38
BillHaggerty Avatar answered Oct 13 '22 11:10

BillHaggerty