我的实际目标是从可执行文件的父目录中读取文件。我首先尝试通过以下操作来解决这个问题。这基本上很好,但当在我的macbook上的Rider中运行时,exeDir解析为/usr/local/share
var exeDir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName);
var parentDir = Directory.GetParent(exeDir).FullName;
原因是Rider通过执行来执行应用程序
/usr/local/share/dotnet/dotnet/用户/thomas/RiderProjects/ConsoleApp1/ConsoleApp1/bin/Debug/netcoreapp3.0/ConsoleApp1.dll
导致Process.GetCurrentProcess().MainModule?.FileName
导致/usr/local/share/dotnet/dotnet
。
在做了一些研究后,我尝试使用其中一个来代替
System.Reflection.Assembly.GetEntryAssembly().Location
System.Reflection.Assembly.GetExecutingAssembly().Location
在windows主机上运行时,我没有像预期的那样报告C:dummyservice
,而是得到了以下结果:
C:\Users\THOMAS~1\AppData\Local\Temp.net\worker\otstv4uv.ifx\worker.dll
这是因为dotnet如何解包捆绑的应用程序,即PublishSingleFile=true
。感谢@OmairMajid在评论中指出这一点。
我仍在寻找如何以独立于平台的方式处理这一问题的建议。
总结一下,这就是我想要实现的目标。dotnet core 3.0辅助应用程序与另一个应用程序捆绑在一起。
C:MyApp
C:MyAppconfig.ini
C:MyApp... # other files (executables, …)
C:MyAppWorkerWorker.exe # installed as service, want to read config.ini above
请尝试一下,在Win中也在.net核心中工作
AppDomain.CurrentDomain.BaseDirectory
[Update]当PublishSingleFIle=true时,Win上也有同样的问题,给定的目录将是C:\Temp.net\ConsoleApp\uxof4qt.col\
关于.net核心github有一个有趣的讨论https://github.com/dotnet/designs/pull/52