Avatar ·

How to get the path to the executable file of a console application in .NET?

Hello everyone! I'm writing a console application in C# (.NET 6), and I need to get the path to the folder where my .exe file is located. I tried several methods, but the results are confusing.\n\nFor example, I run the application from the `C:\\MyApp` folder, but I want it to read the `settings.json` file from the same directory. I tried this:\n\n```csharp\nstring path = Environment.CurrentDirectory;\nConsole.WriteLine(path);\n```\n\nBut when I run the application from the command line while in a different folder, it outputs not `C:\\MyApp`, but the current working directory. This is incorrect.\n\nI also tried:\n\n```csharp\nstring path = Assembly.GetExecutingAssembly().Location;\nConsole.WriteLine(path);\n```\n\nBut this returns the path with the file name (e.g., `C:\\MyApp\\MyApp.exe`), and I only need the folder. Also, I heard that in some