Getting the full path for a Windows service

How to find out the folder where the .exe file for Windows is installed dynamically installed?

Path.GetFullPath(relativePath); 

returns a path based on the C:\WINDOWS\system32 directory.

However, the XmlDocument.Load(string filename) method works against the relative path in the directory where the service .exe file is installed.

+57
c # windows-services
Oct. 14 '08 at 3:49
source share
7 answers

Try

 System.Reflection.Assembly.GetEntryAssembly().Location 
+82
Oct. 14 '08 at 3:57
source share

Try the following:

 AppDomain.CurrentDomain.BaseDirectory 

(Like here: How to find the path to a Windows exe file )

+65
Oct 29 '12 at 20:48
source share
 Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) 
+38
Nov 14 '08 at 14:54
source share

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 an absolute path to the executable.

+5
Oct 14 '08 at 13:38
source share

Another version above:

 string path = Assembly.GetExecutingAssembly().Location; FileInfo fileInfo = new FileInfo(path); string dir = fileInfo.DirectoryName; 
+5
Oct. 14 '08 at 14:24
source share

Environment.CurrentDirectory returns the current directory in which the program is running. In the case of a Windows service, it returns% WINDIR% / system32 in which the executable will be executed, and not where the executable will be executed.

+3
Jun 11 '10 at 20:57
source share

This should indicate the path where the executable is located:

 Environment.CurrentDirectory; 

If not, you can try:

 Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName 

A more hacky but functional way:

 Path.GetFullPath("a").TrimEnd('a') 

:)

-four
Oct 14 '08 at 4:06
source share



All Articles