Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get original executable path for .net core 3.0 single file '/p:PublishSingleFile=true'

Tags:

c#

.net-core

I published a .net core console app with '/p:PublishSingleFile=true' option, but now assembly path is the temporary path where it inflated to.

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)

now returns:

C:\Users\DEFUSER\AppData\Local\Temp\.net\myApp\3dzfa4fp.353\_myApp.json

originally:

C:\devel\myApp\bin\publish\_myApp.json

How can I get the original path of where i put the exe file originally?

thanks in advance!

like image 296
sxagan Avatar asked Oct 17 '19 08:10

sxagan


2 Answers

Based on https://github.com/dotnet/coreclr/issues/25623, which was also confirmed by Scott Hanselman a year later:

  • .NET Core: Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
  • .NET 5: AppContext.BaseDirectory.
like image 154
Grzegorz Smulko Avatar answered Nov 15 '22 08:11

Grzegorz Smulko


There is a new .NET 6.0 property Environment.ProcessPath added exactly for that reason

Returns the path of the executable that started the currently executing process

Design notes can be found here

like image 3
Pavel Anikhouski Avatar answered Nov 15 '22 08:11

Pavel Anikhouski