如何查找动态安装Windows服务.exe文件的文件夹?
Path.GetFullPath(relativePath);
返回基于C:\WINDOWS\system32
目录的路径.
但是,该XmlDocument.Load(string filename)
方法似乎是针对安装服务.exe文件的目录中的相对路径.
尝试
System.Reflection.Assembly.GetEntryAssembly().Location
试试这个:
AppDomain.CurrentDomain.BaseDirectory
(就像这里:如何找到Windows服务exe路径)
Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
这适用于我们的Windows服务:
//CommandLine without the first and last two characters //Path.GetDirectory seems to have some difficulties with these (special chars maybe?) string cmdLine = Environment.CommandLine.Remove(Environment.CommandLine.Length - 2, 2).Remove(0, 1); string workDir = Path.GetDirectoryName(cmdLine);
这应该为您提供可执行文件的绝对路径.
上面的另一个版本:
string path = Assembly.GetExecutingAssembly().Location; FileInfo fileInfo = new FileInfo(path); string dir = fileInfo.DirectoryName;