Reputation: 5873
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
Reputation: 241
You can use ConfigurationManager
to read App.config :
Add a reference to System.Configuration
to your project.
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
Reputation: 1505
Possible options are:
AppDomain.CurrentDomain.BaseDirectory
OR
Assembly.Location
OR
Assembly.GetExecutingAssembly().CodeBase
Personally, I used the last one.
Upvotes: 0
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
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