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

14.8k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
0
On

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.