AppSettingsReader 构造函数

定义

初始化 AppSettingsReader 类的新实例。

public:
 AppSettingsReader();
public AppSettingsReader ();
Public Sub New ()

示例

以下示例创建包含 <appSettings> 节的配置文件,然后使用 AppSettingsReader 读取刚刚生成的设置。

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();  
    }  
}  

以下示例演示上一个示例使用的配置文件。

<?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>  

适用于

另请参阅