【问题标题】:How to Configure App.config file in c#如何在 C# 中配置 App.config 文件
【发布时间】:2012-03-28 22:23:20
【问题描述】:

我正在使用 Visual Studio 2005,并使用“App.config”文件创建了一个应用程序。 当我尝试编辑并向该 App.config 文件添加新值时,它显示错误请帮助我..

我的 app.config 文件包含:

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <appSettings>
   <add key="keyvalue" value="value"/>
    <add key="keyvalue1" value="value1"/>
 </appSettings>
 <mySettings>
   <add name="myname" myvalue="value1"/>
 </mySettings>
</configuration>

显示错误为:

Could not find schema information for the element "mySettings"
Could not find schema information for the element "add"
Could not find schema information for the element "myvalue"

【问题讨论】:

  • 请注意,这些“错误”只是信息性消息。 Visual Studio 只是让您知道,如果这些值应该是元素、属性、应该存储的类型,它就无法解决。忽略这些是安全的,但提供的答案为如何实现读取这些自定义值提供了指导。

标签: c# configuration custom-configuration


【解决方案1】:

不要创建“我的设置”组。将您需要的任何内容放入 AppSettings 组。

您可以创建一个 mySettings 组,但如果您确实包含自定义(非标准)配置部分,则必须在 configSections 元素中声明它们,如 herehere 所述。

我会质疑这是否真的有必要,但是除非有添加自定义部分的真正充分理由,否则请使用我的第一个答案,因为遵循正常标准会更好。它只是让未来的维护程序员更容易。

【讨论】:

    【解决方案2】:

    您正在定义一个不属于正常配置文件的新部分:

     <mySettings> 
       <add name="myname" myvalue="value1"/> 
     </mySettings> 
    

    要合并您自己的部分,您需要编写一些内容来阅读您的特定部分。然后添加对要处理的处理程序的引用,如下所示:

    <configuration>
        <configSections>
           <section name="mySettings" type="MyAssembly.MySettingsConfigurationHander, MyAssembly"/>
        </configSections>
        <!-- Same as before -->
    </configuration>
    

    示例代码示例如下:

    public class MySettingsSection
    {
         public IEnumerable<MySetting> MySettings { get;set; }
    }
    
    public class MySetting
    {
        public string Name { get;set; }
        public string MyValue { get;set; }
    }
    
    public class MySettingsConfigurationHander : IConfigurationSectionHandler
    {
         public object Create(XmlNode startNode)
         {
              var mySettingsSection = new MySettingsSection();
    
              mySettingsSection.MySettings = (from node in startNode.Descendents()
                                             select new MySetting
                                             {
                                                Name = node.Attribute("name"),
                                                MyValue = node.Attribute("myValue")
                                             }).ToList();
    
             return mySettingsSection;
         }
    }
    
    public class Program
    {
        public static void Main()
        {
            var section = ConfigurationManager.GetSection("mySettings") as MySettingsSection;
    
            Console.WriteLine("Here are the settings for 'MySettings' :");
    
            foreach(var setting in section.MySettings)
            {
                Console.WriteLine("Name: {0}, MyValue: {1}", setting.Name, setting.MyValue);
            }
        }
    }
    

    还有其他方法可以读取配置文件,但这是徒手输入的最简单方法。

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2012-02-27
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多