How To Read An App.Config File In C#

IT Support Forum Forums Development C# Development C# Coding How To Read An App.Config File In C#

This topic contains 0 replies, has 1 voice, and was last updated by  Webmaster 6 days, 10 hours ago.

  • Author
    Posts
  • #2169

    Webmaster
    Keymaster

    Here’s how to read an App.Config file in a C# project:

    First add the data that you want to read from your App.Config file to the file using the format below:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
    <add key="Data Name" value="Data Value"/>
    </appSettings>
    </configuration>

    Replace Data Name with the name of the data you want to store and retreive from the App.Config file, and replace Data Value with the corresponding data for that item. You can add multiple Key lines to store different data, each Data Name should be unique. Next you can use the following code example to read the Data Value from the App.Config file:

    var appSettings = ConfigurationManager.AppSettings;
    string dataValue = appSettings[“Data Name”];

    ❗ Note: To use the ConfigurationManager.AppSettings property, you have to add a reference to System.Configuration, otherwise you’ll get an error saying ConfigurationManager Does Not Exist.

You must be logged in to reply to this topic.