GibboK
GibboK

Reputation: 73998

How to use the value of appSettings from App.Config file when creating a Windows Service

I am trying to create a Windows Server. I have some logic in C#

        string urlToPing = ConfigurationSettings.AppSettings["UrlToPing"].ToString();
        Stream data = client.OpenRead(urlToPing);

I need to read

Here my App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="UrlToPing" value="http://mysite.com"/>
  </appSettings>

</configuration>

I am new at Windows Services, my questions:

Upvotes: 38

Views: 83625

Answers (4)

Kubilay
Kubilay

Reputation: 1

  1. Add "using System.Configuration;"
  2. Upper/lower case important
  3. Moreover attention this point; you must use "appSettings" tag that after than "configurations" tags.

Appsettings - screenshot

Upvotes: 0

shambs
shambs

Reputation: 26

Add System.Configuration to references

enter image description here

Upvotes: 0

GibboK
GibboK

Reputation: 73998

To my second question I found a solution:

  1. Add a reference to System.Configuration to your code file.

    using System.Configuration;

  2. The setting may now be referenced correctly...

    ConfigurationManager.AppSettings["UrlToPing"].ToString();

Upvotes: 61

wacdany
wacdany

Reputation: 1001

To your first question, when you build a executable project (Windows Service, Console Application, etc) it will rename the app.config to "YourApplication".exe.config where "YourApplication" is the name of your Startup assembly. It will then copy the file to your output folder.

Upvotes: 4

Related Questions