在app.config中读取章节问题

本文关键字:问题 读取 app config | 更新日期: 2023-09-27 18:16:33

我把我的app.config分成如下几个部分:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="databaseConnectionStrings" type="System.Configuration.NameValueSectionHandler" />
    <section name="dataDictionary" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <databaseConnectionStrings>
    <add key="ILFSsqlServer" value="ODBC;DSN=sql server copycloas;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=ILFSView;"/>
  </databaseConnectionStrings>
  <dataDictionary>
    <add key="CLOASEUCDBA_T_CONTACT" value="CLOASEUCDBA.T_Contact" />
  </dataDictionary>
</configuration>

从我可以告诉xml应该工作良好,但当我尝试调用它使用:

var section2 = ConfigurationManager.GetSection("dataDictionary") as NameValueConfigurationCollection;
var result2 = section2["CLOASEUCDBA_T_CONTACT"];
Console.WriteLine(result2);

我在第二行得到一个空引用异常。

在app.config中读取章节问题

似乎您必须将类型强制转换为AppSettingsSection而不是NameValueCollection

来自MSDN: ConfigurationManager。GetSection方法

// Create the AppSettings section. 
// The function uses the GetSection(string)method  
// to access the configuration section.  
// It also adds a new element to the section collection. 
public static void CreateAppSettings() {
    // Get the application configuration file.
    System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);
    string sectionName = "appSettings";
    // Add an entry to appSettings. 
    int appStgCnt =
        ConfigurationManager.AppSettings.Count;
    string newKey = "NewKey" + appStgCnt.ToString();
    string newValue = DateTime.Now.ToLongDateString() + " " 
                          + DateTime.Now.ToLongTimeString();
    config.AppSettings.Settings.Add(newKey, newValue);
    // Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified);
    // Force a reload of the changed section. This  
    // makes the new values available for reading.
    ConfigurationManager.RefreshSection(sectionName);
    // Get the AppSettings section.
    AppSettingsSection appSettingSection =
        (AppSettingsSection)config.GetSection(sectionName);
    Console.WriteLine();
    Console.WriteLine("Using GetSection(string).");
    Console.WriteLine("AppSettings section:");
    Console.WriteLine(
    appSettingSection.SectionInformation.GetRawXml());
}

注意Console.WriteLine()之前调用config.GetSection()方法。

我经常使用的另一种方法是通过如下方式访问项目属性。
using Namespace.Properties;
public class MyClass {
    public string MySettingMeaningfulName { 
        get { return Settings.Default.ClOASEUCDBA_T_CONTACT; }
    }
}
NameValueCollection代替NameValueConfigurationCollection解决

我看到你将ConfigurationManager.GetSection("dataDictionary")转换为NameValueConfigurationCollection。将强制转换为System.Collections.Specialized.NameValueCollection

    [Test]
    public void Test_NameValueConfigurationSection()
    {
        var section2 = ConfigurationManager.GetSection("dataDictionary") as NameValueCollection;
        var result2 = section2["CLOASEUCDBA_T_CONTACT"];
        Assert.AreEqual("CLOASEUCDBA.T_Contact", result2);
    }