Windows服务 – 获取当前目录
我有一个Windows服务应该在当前目录中查找configuration文件。
所以我使用directory.getcurrentdirectiry()而不是我回来的服务目录 
 c:\windows\system32 
任何想法为什么以及如何得到服务目录?
 不要使用Directory.GetCurrentDirectory() 。 我有与C:\ Windows \ System32被返回相同的确切问题。 用这个代替: 
 Path.GetDirectoryName(Application.ExecutablePath); 
您可以通过在代码中包含以下行来将当前目录设置为您的服务所在的目录:
 System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory); 
这个重要的部分是:
 System.AppDomain.CurrentDomain.BaseDirectory 
这将返回到您的服务运行目录的path。
尝试这个:
 System.Reflection.Assembly.GetEntryAssembly().Location 
从完整path获取目录:
 var location = System.Reflection.Assembly.GetEntryAssembly().Location; var directoryPath = Path.GetDirectoryName(location); 
当写一个Windows服务比较相当愚蠢的问题:)
 string applicationDir = AppDomain.CurrentDomain.BaseDirectory;