MoS*_*She 50 c# windows-services working-directory
我有一个Windows服务,应该在其当前目录中查找配置文件.
所以我使用directory.getcurrentdirectiry()但不是服务目录我回来了
c:\windows\system32
Run Code Online (Sandbox Code Playgroud)
知道为什么以及如何获得服务目录?
Jed*_*Jed 127
您可以通过在代码中包含以下行来将当前目录设置为运行服务的目录:
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
Run Code Online (Sandbox Code Playgroud)
其中重要的部分是:
System.AppDomain.CurrentDomain.BaseDirectory
Run Code Online (Sandbox Code Playgroud)
这将返回运行服务的目录的路径.
cod*_*der 30
试试这个:
System.Reflection.Assembly.GetEntryAssembly().Location
Run Code Online (Sandbox Code Playgroud)
Mic*_*nnt 16
从完整路径获取目录:
var location = System.Reflection.Assembly.GetEntryAssembly().Location;
var directoryPath = Path.GetDirectoryName(location);
Run Code Online (Sandbox Code Playgroud)
与编写Windows服务相比,这是一个非常愚蠢的问题:)
小智 12
不要用Directory.GetCurrentDirectory().我有与C:\ Windows\System32返回相同的问题.请改用:
Path.GetDirectoryName(Application.ExecutablePath);
| 归档时间: |
|
| 查看次数: |
54061 次 |
| 最近记录: |