tags:

views:

174

answers:

3

Hi,

I have a service in C#, it uses a config.xml file for configuration. I want to deplay the xml along side with the service executable. But I can't seem to understand where to find the service exe installed location at runtime so that I could find/load the config.

Help please.

+3  A: 

The static method Assembly.GetEntryAssembly() will give you a reference to the entry assembly (the .exe file), and the Location property will give you the location of the file:

Assembly.GetEntryAssembly().Location

Another way around, if you know of a type in the entry assembly, is to use the Type.Assembly to get a reference to the assembly:

typeof(Program).Assembly.Location

If you need just the directory path, use the static Path.GetDirectoryName() method.

Off topic: have you considered the configuration API built into .NET? I am not saying this will be better in your specific case, but I guess it is worth considering before rolling your own configuration framework.

Jørn Schou-Rode
A: 

Do you mean that you are building a project in visual studio and you want to know where your compiled EXE is saved to? You will find this in "[project folder]\bin\debug".

Or do you mean you already have a service installed on your computer and you want to know where its running from? To do this you can right click on the service and choose properties. This will show the path of the file.

TheSean
A: 

Your executable is located in AppDomain.CurrentDomain.BaseDirectory.

Some of my service projects load a custom log4net configuration like this.

var file = new FileInfo( AppDomain.CurrentDomain.BaseDirectory + "/" + filename );
if (file.Exists) {
    XmlConfigurator.ConfigureAndWatch( file );    
}

The same service uses an normal app.config file for standard configuration.

Lachlan Roche