Home > Software design >  My .net core published executable gives a different directory path than when running the code
My .net core published executable gives a different directory path than when running the code

Time:10-13

I created a dotnet core console that processes txt files. It needs to be located in the same folder as the txt files in order to work because it runs through all txt files, so defining its current directory is needed. When I run my solution in Visual Studio, it gives the right directory where my app is, but when I publish it as self-contained console and run it from Desktop for example, it gives a wrong directory (C:\users...\cue2n1g0.eiw). I am using the following line to define the current directory of the console.

 string currentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

CodePudding user response:

In my tests all these worked out the same and returned the true path of the desktop, for my .net 5 console app published there using VS's "publish to folder" option:

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
AppContext.BaseDirectory
Environment.CurrentDirectory
  • Related