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 ?

1

4 Answers

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).

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;

Possible options are:

AppDomain.CurrentDomain.BaseDirectory

OR

Assembly.Location

OR

Assembly.GetExecutingAssembly().CodeBase

Personally, I used the last one.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest