Reading settings from app.config or web.config in .NET
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In .NET applications, managing application-specific settings is crucial for creating flexible and maintainable code. You can store and retrieve these settings in multiple ways, one of the common methods being through app.config or web.config files. These XML-based configuration files store settings that are read at runtime, allowing for changes in behavior without altering the codebase. Here, we will explore how to work with these config files, focusing on key aspects that help manipulate and access configuration data effectively.
Understanding app.config and web.config
app.config is used in Windows Forms, Console Apps, and WPF applications, while web.config is employed in ASP.NET projects. These files typically reside in the root directory of your project. Although the structure of these files can be complex depending on the application's needs, the most common elements include appSettings, connectionStrings, and custom configuration sections.
Standard Sections in Configuration Files
- appSettings: This is used for storing key-value pairs. It is ideal for simple configurations such as feature toggles or environment-specific variables.
- connectionStrings: Specifically designed for storing connection strings to databases or other network resources.
Reading from appSettings
To access the values in appSettings, you use the ConfigurationManager class located in the System.Configuration namespace. Here’s an example:
Reading from connectionStrings
The connectionStrings section is accessed similarly, but it returns a ConnectionStringSettings object:
Custom Configuration Sections
For more complex scenarios, you can define custom configuration sections to structure your settings more comprehensively. Here's how you can define and read these custom sections:
- Define the section in the config file:
- Create a class to represent the section:
- Read the section in your application:
Tips for Secure Configuration Management
- Avoiding Sensitive Data: Never store passwords or other sensitive information directly in configuration files. Use environment variables or secure services like Azure Key Vault instead.
- Encryption: For desktop applications, consider encrypting sensitive sections of your configuration file using the
aspnet_regiistool. - Access Control: Ensure the configuration files are not accessible via a web server for web applications.
Summary Table
| Feature | Description | Example Usage |
appSettings | Stores key-value pairs for simple configurations. | ConfigurationManager.AppSettings |
connectionStrings | Manages database or network connections strings. | ConfigurationManager.ConnectionStrings |
| Custom Sections | Allows complex and structured configuration data. | Define custom classes and XML sections |
In conclusion, .NET’s configuration system provides a robust and versatile approach to managing application settings. By understanding and utilizing the different types of configuration sections and adhering to best practices for security, developers can build efficient, secure, and maintainable applications.
Related reading
- Reading settings from app.config or web.config in .NET
- ReadOnlyCollection or IEnumerable for exposing member collections?
- Recommendation Algorithms for tweets in C
- Recommended Open Source C algorithms data structures libraries
- Reducing memory usage of .NET applications?
- Ref folder within .NET 5.0 bin folder
- Ref in async Task
- Refactoring Backgroundworker to async/await

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.