views:

98

answers:

3

Hi folks,

i have installed my own custom Windows Service. I need to find out the physical path, where the service exists.

eg.

log4net.Config.XmlConfigurator.Configure(
    new System.IO.FileInfo(<insert path here> + "log4net.config"));

Any ideas?

A: 

System.Environment.CurrentDirectory?

Actually the above doesn't work but this does:

string servicePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().Location);
Wim Hollebrandse
I think all services are started in %WINDIR%\system32
Luke Quinane
Yep, hence my edit.
Wim Hollebrandse
Nope - also not right. That's the path + filename.... :(
Pure.Krome
Sure, but once you get that, you can get the path using the GetDirectoryName static method o the Path class in System.IO.
Wim Hollebrandse
Hmm. ok .. is that better than my suggested answer? or are they both the same thing .. ??
Pure.Krome
+2  A: 
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Alex Reitbort
Nope - this isn't correct. It returns the location/path AND the exe name.... ????
Pure.Krome
How about now ?
Alex Reitbort
How does that compare to my suggested answer? just another way but will also work?
Pure.Krome
Check MSDN: AppDomain.CurrentDomain.BaseDirector - Gets the base directory that the assembly resolver uses to probe for assemblies.Assembly.GetExecutingAssembly().Location - returns the path and name of currently executing assembly.
Alex Reitbort
Awesome - thanks :)
Pure.Krome
A: 
Pure.Krome