【问题标题】:ConfigurationManager to get project config dataConfigurationManager 获取项目配置数据
【发布时间】:2010-12-16 09:09:28
【问题描述】:

我在我的项目中创建了一个配置文件,并将其命名为 MyConfig.config。它包含以下内容:

<configuration>
  <MySection MyString="StringHere"/>
</configuration>

我正在尝试按如下方式访问它:

AppSettingsSection settings = (AppSettingsSection)ConfigurationManager.GetSection("MySection");
string myString = settings.Settings["MyString"].Value;

显然我做错了什么。甚至可以这样使用ConfigurationManager吗?

【问题讨论】:

    标签: c# configuration configurationmanager


    【解决方案1】:

    是的,这是可能的。有几个步骤。首先,您必须在配置中定义您的部分。这涉及声明部分名称和类型(您编写的类):

    <configSections>
       <sectionGroup name="system.web">
          <section name="myConfig" type="MyConfig.MyConfigSectionHandler,MyConfig" />
       </sectionGroup>
    </configSections>
    

    接下来,您必须实际编写代码来处理该部分。它必须从 IConfigurationSectionHandler 继承:

    using System;
    using System.Web;
    using System.Xml;
    using System.Configuration;
    
    namespace MyConfig
    {
       public enum LevelSetting
       {
          High,
          Medium,
          Low,
          None
       }
       public class MyConfigSectionHandler : IConfigurationSectionHandler
       {
          public virtual object Create(object parent,object configContext,XmlNode section)
          {
             int iLevel = 0;
             string sName = "";
    
             ConfigHelper.GetEnumValue(section, "level", typeof(LevelSetting), ref iLevel);
             ConfigHelper.GetStringValue(section,"name",ref sName);
             return new MyConfigSection((LevelSetting)iLevel,sName);
          }
       }
       public class MyConfigSection
       {
          private LevelSetting level = LevelSetting.None;
          private string name = null;
    
          public MyConfigSection(LevelSetting _level,string _name)
          {
             level = _level;
             name = _name;
          }
          public LevelSetting Level
          {
             get {return level;}
          }
          public string Name
          {
             get {return name;}
          }
       }
       internal class ConfigHelper
       {
          public static XmlNode GetEnumValue
          (XmlNode _node, string _attribute,Type _enumType, ref int _val)
          {
             XmlNode a = _node.Attributes.RemoveNamedItem(_attribute);
             if(a==null)
                throw new ConfigurationException("Attribute required: " + _attribute);
             if(Enum.IsDefined(_enumType, a.Value))
                _val = (int)Enum.Parse(_enumType,a.Value);
             else
                throw new ConfigurationException("Invalid Level",a);
             return a;
          }
          public static XmlNode GetStringValue(XmlNode _node, string _attribute, ref string _val)
          {
             XmlNode a = _node.Attributes.RemoveNamedItem(_attribute);
             if(a==null)
                throw new ConfigurationException("Attribute required: " + _attribute);
             else
                _val = a.Value;
             return a;      
          }
       }
    }
    

    接下来,将实际配置项添加到web.config中:

    <system.web>
        <myConfig level="High" name="hello world" />
    </system.web>
    

    完成。

    【讨论】:

      【解决方案2】:

      您的配置文件是否被复制到输出目录并重命名为 YourExeFile.exe.config?据我所知,您必须将配置文件命名为 app.config 或在构建后自行复制。

      当您想从替代配置文件加载配置时,您可以使用ConfigurationMangager.OpenMappedConfigurationFile() 方法加载该文件而不是默认配置文件。示例见MSDN

      当您想使用自定义的用户定义配置部分时,您必须提供类来访问该部分。看看this good article 开始吧。

      【讨论】:

        【解决方案3】:

        我认为您不能以这种方式使用配置文件。您是否考虑过使用 .settings 文件?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-10
          • 1970-01-01
          • 2014-06-20
          • 1970-01-01
          • 2019-04-17
          • 2015-01-22
          • 1970-01-01
          • 2015-12-05
          相关资源
          最近更新 更多