I have a directory which contains a self-contained "exe" and a config file. The exe has to read this config file. But the problem is that the exe can not get the right path of this config file.
Let me first talk about how I did it.
- Create a NET5.0 project in VS2019
- Add codes
using System;
using System.IO;
using System.Reflection;
namespace TestFile {
class Program {
static void Main(string[] args) {
string assemblyLocation = Assembly.GetEntryAssembly().Location;
Console.WriteLine($"assemblyLocation=\"{assemblyLocation}\"");
string configFile = Path.Combine((new FileInfo(assemblyLocation)).DirectoryName, "test.conf");
Console.WriteLine($"configFile=\"{configFile}\"");
File.ReadAllText(configFile);
}
}
}
- Publish this project to a "SelfContained" and "PublishSingleFile" exe
- Add file "test.conf" in the directory where the exe locates
- Run this exe
How can I get the right path of this config file?