How can I find out the folder where the windows service .exe file is installed dynamically?
returns a path based on
XmlDocument.Load(string filename) method appears to be working against relative path inside the directory where the service .exe file is installed to.
(Just like here: How to find windows service exe path)
This works for our windows service:
//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);
This should give you the absolute path of the executable.
Another version of the above:
string path = Assembly.GetExecutingAssembly().Location; FileInfo fileInfo = new FileInfo(path); string dir = fileInfo.DirectoryName;
Environment.CurrentDirectory returns current directory where program is running. In case of windows service, returns %WINDIR%/system32 path that is where executable will run rather than where executable deployed.
This should give you the path that the executable resides in:
If not, you could try:
A more hacky, but functional way: