简体   繁体   English

System.Configuration with Separate .config文件

[英]System.Configuration with Separate .config file

I took the code from this example. 我从这个例子中获取了代码。

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

What I am wondering (and I've been all over the internet today looking) ... How do you get that to be in an external(separate) file. 我在想什么(今天我一直在互联网上寻找)...你如何得到它在一个外部(单独)文件。

The idea I'm going for is: 我想要的想法是:

<configuration>
  <configSections>
    <sectionGroup name="pageAppearanceGroup">
      <section
        name="pageAppearance"
        type="HelperAssembly.Configuration.PageAppearanceSection,HelperAssembly"
        allowLocation="true"
        allowDefinition="Everywhere"
      />
    </sectionGroup>
  </configSections>


  <pageAppearanceGroup fileName="SomeSeparateFile.config"/>

</configuration>

.................. ..................

The above does not work (of course). 以上不起作用(当然)。

The below is my copying/pasting of the ms article I mention above. 以下是我上面提到的ms文章的复制/粘贴。 And it was fully functioning when I pasted it here. 当我把它贴在这里时,它完全正常运作。

//START HelperAssembly.csproj

namespace HelperAssembly.Configuration
{
    using System;
    using System.Collections;
    using System.Text;
    using System.Configuration;
    using System.Xml;

    public class PageAppearanceSection : ConfigurationSection
    {
        // Create a "remoteOnly" attribute.
        [ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
        public Boolean RemoteOnly
        {
            get
            {
                return (Boolean)this["remoteOnly"];
            }
            set
            {
                this["remoteOnly"] = value;
            }
        }

        // Create a "font" element.
        [ConfigurationProperty("font")]
        public FontElement Font
        {
            get
            {
                return (FontElement)this["font"];
            }
            set
            { this["font"] = value; }
        }

        // Create a "color element."
        [ConfigurationProperty("color")]
        public ColorElement Color
        {
            get
            {
                return (ColorElement)this["color"];
            }
            set
            { this["color"] = value; }
        }
    }

    // Define the "font" element
    // with "name" and "size" attributes.
    public class FontElement : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "Arial", IsRequired = true)]
        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
        public String Name
        {
            get
            {
                return (String)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("size", DefaultValue = "12", IsRequired = false)]
        [IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 6)]
        public int Size
        {
            get
            { return (int)this["size"]; }
            set
            { this["size"] = value; }
        }
    }

    // Define the "color" element 
    // with "background" and "foreground" attributes.
    public class ColorElement : ConfigurationElement
    {
        [ConfigurationProperty("background", DefaultValue = "FFFFFF", IsRequired = true)]
        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
        public String Background
        {
            get
            {
                return (String)this["background"];
            }
            set
            {
                this["background"] = value;
            }
        }

        [ConfigurationProperty("foreground", DefaultValue = "000000", IsRequired = true)]
        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
        public String Foreground
        {
            get
            {
                return (String)this["foreground"];
            }
            set
            {
                this["foreground"] = value;
            }
        }

    }

}



    namespace HelperAssembly.Configuration
{
    using System;
    using System.Configuration;

    public static class ConfigurationRetriever
    {
        public static PageAppearanceSection RetrievePageAppearanceSection1()
        {
            PageAppearanceSection config = (PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");
            return config;
        }
}
}



//START ConsoleApplication1.csproj

    using System;

    using HelperAssembly.Configuration;

    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {

            try
            {
                PageAppearanceSection pas = ConfigurationRetriever.RetrievePageAppearanceSection1();
                if (null != pas)
                {
                    Console.WriteLine(pas.Color.Foreground);
                    Console.WriteLine(pas.Color.Background);
                }
            }

            catch (Exception ex)
            {
                Exception innerException = ex;
                while (null != innerException)
                {
                    Console.WriteLine(innerException.Message);
                    Console.WriteLine("\n\r");

                    Console.WriteLine(innerException.StackTrace);
                    Console.WriteLine("\n\r");

                    innerException = innerException.InnerException;
                }
            }

            Console.WriteLine("Press Enter");
            Console.ReadLine();

        }
    }
    }



//XML for config file that works with the above code


    <?xml version="1.0" encoding="utf-8" ?>
     <configuration>
       <configSections>
         <sectionGroup name="pageAppearanceGroup">
           <section
        name="pageAppearance"
        type="HelperAssembly.Configuration.PageAppearanceSection,HelperAssembly"
        allowLocation="true"
        allowDefinition="Everywhere"
      />
     </sectionGroup>
    </configSections>

    <pageAppearanceGroup>
    <pageAppearance remoteOnly="true">
      <font name="TimesNewRoman" size="18"/>
      <color background="DEFDEF" foreground="ABCABC"/>
      </pageAppearance>
     </pageAppearanceGroup>

    </configuration>

This will work if you change your app.config to use this: 如果您更改app.config以使用此选项,这将有效:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="pageAppearanceGroup">
      <section
        name="pageAppearance"
        type="HelperAssembly.Configuration.PageAppearanceSection,HelperAssembly"
        allowLocation="true"
        allowDefinition="Everywhere"
      />
    </sectionGroup>
  </configSections>

  <pageAppearanceGroup>
    <pageAppearance configSource="SomeSeparateFile.config"/>
  </pageAppearanceGroup>

</configuration>

And your someSeparateFile.config to look like this: 你的someSeparateFile.config看起来像这样:

<pageAppearance remoteOnly="true">
  <font name="TimesNewRoman" size="18"/>
  <color background="123456" foreground="ABCDEF"/>
</pageAppearance>

(no configuration element in this file!) (此文件中没有configuration元素!)

I've been able to move configSections into separate files. 我已经能够将configSections移动到单独的文件中。 Not sure you can do that with configGroups unless you do a lot more programming. 除非你做更多的编程,否则不确定你能用configGroups做到这一点。 The configuration framework model lets you move configSections out pretty easily. 配置框架模型可以让您轻松地移动configSections。

Hopefully this helps!! 希望这有助于!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.configuration web.config和app.config - System.configuration web.config and app.config 系统.配置配置管理器 - System.Configuration Configuration Manager 如何在不使用System.Configuration的情况下以编程方式反序列化xml文件 - How To Deserialize xml File programically without System.Configuration 如果没有本地app.config,是否可以使用System.Configuration? 以及如何处理(如果可能)? - Is it possible to use System.Configuration, if there is no local app.config? And how to, if it is possible? 无法将 .NET 配置部分加载到 aws 中的 System.Configuration 类中 lambda function - Unable to load .NET config sections into System.Configuration classes within an aws lambda function 定制安装程序类中的System.Configuration - System.Configuration in a custom installer class 与XMLSerializer相比,System.Configuration有什么好处? - What are the benefits of System.Configuration over XMLSerializer? 是否可以使用System.Configuration读取XML - Is it possible to read XML using System.Configuration 尽管使用System.Configuration仍无法识别ConfigurationManager - ConfigurationManager unrecognized despite using System.Configuration 命名空间 System.Configuration 中不存在 configurationManager - configurationManager does not exist in the namespace System.Configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM