Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
This article is specific to .NET Framework. It doesn't apply to newer implementations of .NET, including .NET 6 and later versions.
This article shows you how to add a simple setting to an App.config file in a .NET Framework app, and then read the value programmatically. Instead of just reading a single value, you can read an entire section or the entire file. For more examples and information, see the ConfigurationManager docs.
Visual Studio makes it easy to add an App.config file to your project. After creating a .NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.
Open the App.config file and add the following XML within the <configuration>
element.
<appSettings>
<add key="occupation" value="dentist"/>
</appSettings>
To access the setting's value in your code, get the value by indexing into the AppSettings property. The AppSettings property makes it easy to obtain data from the <appSettings>
element of your configuration file.
string occupation = ConfigurationManager.AppSettings["occupation"];
While it's straightforward to use configuration files for executable apps, it's a little more complicated for class libraries. Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App.config file. Even if you distribute an App.config file alongside your library's assembly file, the library code will not read the file. Alternatively, consider the following ways to use configuration settings in a class library:
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining