AppSettingsReader Class
.NET Framework 4
Provides a method for reading values of a particular type from the configuration.
Namespace:
System.Configuration
Assembly: System (in System.dll)
The AppSettingsReader type exposes the following members.
| Name | Description | |
|---|---|---|
|
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
|
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
|
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
|
GetType | Gets the Type of the current instance. (Inherited from Object.) |
|
GetValue | Gets the value for a specified key from the AppSettings property and returns an object of the specified type containing the value from the configuration. |
|
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
|
ToString | Returns a string that represents the current object. (Inherited from Object.) |
The following example creates a configuration file that contains the <appSettings> section, and then uses the AppSettingsReader to read the settings just generated.
using System; using System.Collections.Specialized; using System.Configuration; class UsingAppSettingsReader { static void DisplayAppSettings() { try { AppSettingsReader reader = new AppSettingsReader(); NameValueCollection appSettings = ConfigurationManager.AppSettings; for (int i = 0; i < appSettings.Count; i++) { Console.WriteLine("Key : {0} Value: {1}", appSettings.GetKey(i), appSettings[i]); } } catch (ConfigurationErrorsException e) { Console.WriteLine("[DisplayAppSettings: {0}]", e.ToString()); } } static void CreateAppSettings() { try { // Get the count of the Application Settings. int appSettingsCount = ConfigurationManager.AppSettings.Count; string asName = "Key" + appSettingsCount.ToString(); // Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add an Application setting. config.AppSettings.Settings.Add(asName, DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString()); // Save the configuration file. config.Save(ConfigurationSaveMode.Modified); // Force a reload of a changed section. ConfigurationManager.RefreshSection("appSettings"); } catch (ConfigurationErrorsException e) { Console.WriteLine("[CreateAppSettings: {0}]", e.ToString()); } } static void Main(string[] args) { string selection = ""; while (selection.ToLower() != "q") { // Create appSettings section. CreateAppSettings(); // Display appSettings section. DisplayAppSettings(); Console.WriteLine(); Console.WriteLine("Enter 'Q' to exit or press enter to continue."); Console.Write("> "); selection = Console.ReadLine(); } } }
The following example demonstrates a configuration file used by the previous example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="AppStg0" value="Thursday, December 01, 2005 12:53:56 PM" />
<add key="AppStg1" value="Thursday, December 01, 2005 12:54:15 PM" />
<add key="AppStg2" value="Thursday, December 01, 2005 1:37:22 PM" />
</appSettings>
</configuration>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Total bull****
Yeah, the example is irrelevant, and the hole System.Configuration namespace is a mess.
- 2/23/2012
- BOBAH123
The sample is not relate to AppSettingsReader
The sample is not relate to AppSettingsReader, we need show how to use AppSettingsReader class to read config settings.
- 9/12/2011
- Tran Phuc Khanh
Add System.Configuration as a reference
After you add the reference you will have access to the ConfigurationManager.
- 9/4/2011
- Oguzhan Filizlibay
So complex, even the example given is wrong
You have to know that this System.Configuration namespace is so complex now (it is virtually unusable), when the example given a) doesn't even work (ConfiguationManager no longer exists), and b) the AppSettingsReader reader instantiated on line 13 of the example is never even used!
$0$0
$0
$0Do we need to just go back to ini files! or can we please get a simple app config management system that can easily read/write local settings to a local file. $0
- 4/19/2011
- Larker1
Example code is not relevant to the class
As others have pointed out, the example code is not relevant to the class!
- 3/31/2011
- rohancragg
absolutely irrelevant example
I just saw this class and curious to know what difference it makes from ConfigurationManager.appSettings. I think it is useful to read type safe values from appSettings instead of just string values. This example is absolutely irrelevant.
Useless example
AppSettingsReader reader = new AppSettingsReader(); - What is the role of this string, the reader is never used. The example has no relation to the class described.
- 11/15/2010
- Andriy Savin