TonyP
TonyP

Reputation: 5873

What is the equivalent of Application.ExecutablePath in Windows Service

In a windows forms application, I use Application.Executable path to get to the App.config.,

I need to get to app.config in Windows Service.. What would that be ?

Upvotes: 6

Views: 16894

Answers (4)

SerkanOzvatan
SerkanOzvatan

Reputation: 241

You can use ConfigurationManager to read App.config :

  1. Add a reference to System.Configuration to your project.

  2. Use ConfigurationManager like this to read App.config :

    ConfigurationManager.AppSettings["KeySample"]
    

    In the config file you can add your keys like this :

    <configuration>
        <appSettings>
           <add key="KeySample" value="SampleValue"/>
        </appSettings>
    </configuration>
    

    So you don't have to get the path.

Upvotes: 0

Nasir Mahmood
Nasir Mahmood

Reputation: 1505

Possible options are:

AppDomain.CurrentDomain.BaseDirectory

OR

Assembly.Location

OR

Assembly.GetExecutingAssembly().CodeBase

Personally, I used the last one.

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166396

Have a look at AppDomain.BaseDirectory Property

Gets the base directory that the assembly resolver uses to probe for assemblies.

The most common usage would be similar to

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

Upvotes: 3

Brad Christie
Brad Christie

Reputation: 101604

A couple of options:

System.Reflection.Assembly.GetExecutingAssembly().Location

For the current assembly. or, you can derive it from a type:

System.Reflection.Assembly.GetAssembly(typeof(MyAssemblyType)).Location

Then (on either) you can use Path.GetDirectoryName to get the folder it's originating from (assuming your app.config is within that same directory).

Upvotes: 11

Related Questions