[C# Windows Service] Getting the absolute path of a config file

I have a Windows Service which tracks the performance of the database

and application server and based on some user defined values which is

lifted from a user defined XML config file, the performance of the

servers are benchmarked against these user defined values.

Now the problem is that i was having the config file in a fixed

location (C:\UserConfig.config) and I hard code the location into the

XMLTextReader

XmlTextReader reader = new XmlTextReader(constObj.XML_FILE_PATH);

As per the requirements, the config file should be along with the

solution file and as the position of the solution file can change, i am

now not able to hard code those values.

I tried the Path class from the System.IO namespace

string configFilePath = Path.GetFullPath("CounterService\\CounterServiceData.config");

but even now it shows "C:\\SYSROOT\\system32\\" this as the current directory for the service. Is there any other way to get the absolute path of the config file?

Even the"Directory.GetDirectories" seems to search from the current directory which is "C:\\SYSROOT\\system32\\" eventhough the service is located elsewhere.

Kindly help me out if i was clear in explaining the scenario.

[1443 byte] By [AmrishDeep] at [2007-12-26]
# 1

The SCM starts your service so the current directory will be the SCM directory (System32). However you can always get the directory where your service is running by using this:

Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

A hack, yes. But it works.

Michael Taylor - 11/7/06

TaylorMichaelL at 2007-9-4 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2
I found one other way which might also be useful,

System.Reflection.Assembly.GetExecutingAssembly().Location;

AmrishDeep at 2007-9-4 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 3
None of these works, if you have installed the assembly in the GAC!
Just-A-Nerd at 2007-9-4 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified