AppSettings get value from .config file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reading AppSettings from a .config file is straightforward in .NET, but production bugs appear when keys are missing, values are malformed, or environment overrides are unclear. The safest approach is to centralize configuration reads, validate early, and fail fast for required settings. This keeps runtime behavior predictable across development and deployment.
Basic Read from app.config or web.config
For .NET Framework apps, ConfigurationManager.AppSettings is the standard API.
Example config file:
This works for classic console, desktop, and ASP.NET Framework applications.
Parse and Validate Typed Values
AppSettings returns strings. Parse once near startup and validate required keys.
This avoids scattered string parsing and inconsistent defaults.
Centralize Access in One Configuration Service
Instead of reading keys everywhere, create a small typed configuration class.
Centralization improves testability and makes key ownership obvious.
Environment-Specific Config Files
In classic .NET deployments, transforms can provide environment-specific values. Keep key names consistent and only vary values.
Practical guidance:
- store non-secret environment values in config transforms.
- keep secrets out of source-controlled
.configfiles. - use secure secret stores or deployment-time injection for credentials.
This pattern reduces accidental secret leaks.
Handling Missing or Optional Keys
Not all keys must be mandatory. For optional keys, define explicit defaults in one place.
Avoid silent defaults for keys that affect safety, billing, or data integrity.
Debugging Configuration Issues
When values seem wrong:
- confirm which config file is loaded at runtime.
- log effective non-sensitive settings at startup.
- verify transform output in build artifacts.
Lightweight startup diagnostics can save hours of incident triage.
Do not print secrets in logs.
.NET Core Note
If your app uses modern .NET configuration with appsettings.json, use IConfiguration and options binding instead of ConfigurationManager. The title here is about .config files, so choose API by runtime model rather than mixing both in one application.
Unit Testing Configuration Readers
To keep configuration logic testable, isolate parsing from static global calls. You can read raw strings once and pass them into a parser function that unit tests can cover without loading real config files.
This prevents hard-to-test static configuration dependencies from spreading through business code.
Common Pitfalls
- Reading the same keys in many classes with duplicated parsing logic.
- Assuming all values in AppSettings are valid without validation.
- Keeping secrets in source-controlled
.configfiles. - Relying on implicit defaults for required operational settings.
- Mixing classic
.configAPIs and modern options patterns inconsistently.
Summary
- Use
ConfigurationManager.AppSettingsfor classic.configscenarios. - Parse and validate values once at startup.
- Centralize configuration access in typed helpers.
- Keep environment overrides explicit and secrets external.
- Treat configuration as part of application correctness, not only deployment detail.

