AppSettingsReader 类

定义

提供一种从配置文件中读取特定类型的值的方法。Provides a method for reading values of a particular type from the configuration.

public ref class AppSettingsReader
public class AppSettingsReader
type AppSettingsReader = class
Public Class AppSettingsReader
继承
AppSettingsReader

示例

下面的示例创建一个包含节的配置文件 <appSettings> ,然后使用 AppSettingsReader 读取刚刚生成的设置。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.Configuration;  

class Program  
{  
    static void Main(string[] args)  
    {  
        var reader = new AppSettingsReader();  

        var stringSetting = reader.GetValue("String setting", typeof(string));  
        Console.WriteLine("String setting: " + stringSetting);  

        var dateTimeSetting = reader.GetValue("DateTime setting", typeof(DateTime));  
        Console.WriteLine("DateTime setting: " + dateTimeSetting);  

        try  
        {  
            var missingSetting = reader.GetValue("Int setting", typeof(Int32));  
        }  
        catch (InvalidOperationException e)  
        {  
            Console.WriteLine("Missing key error: " + e.Message);  
        }  

        Console.WriteLine("Press any key to continue");  
        Console.ReadKey();  
    }  
}  

下面的示例演示上一示例所使用的配置文件。The following example demonstrates a configuration file used by the previous example.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
    <appSettings>  
        <add key="String setting" value="String retrieved from App.Config"/>  
        <add key="Date setting" value="Thursday, December 01, 2005 12:53:56 PM"/>  
    </appSettings>  
</configuration>  

构造函数

AppSettingsReader()

初始化 AppSettingsReader 类的新实例。Initializes a new instance of the AppSettingsReader class.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetValue(String, Type)

AppSettings 属性中获取指定键的值,并返回指定类型的对象,该对象包含来自配置文件的值。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()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅