c# - 带有单独 .config 文件的 System.Configuration

标签 c# configuration-files

我从这个例子中获取了代码。

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

我想知道的是(我今天在网上到处找)...... 你如何把它放在一个外部(单独的)文件中。

我的想法是:

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


  <pageAppearanceGroup fileName="SomeSeparateFile.config"/>

</configuration>

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

上面的方法是行不通的(当然)。

下面是我对上面提到的ms文章的复制/粘贴。 当我将其粘贴到此处时,它可以正常运行。

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

最佳答案

如果您更改 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>

你的 someSeparateFile.config 看起来像这样:

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

(此文件中没有配置元素!)

我已经能够将 configSections 移动到单独的文件中。除非你做更多的编程,否则不确定你可以用 configGroups 做到这一点。配置框架模型让您可以非常轻松地将 configSections 移出。

希望这对您有所帮助!

关于c# - 带有单独 .config 文件的 System.Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3366199/

相关文章:

bash - 使用 sed/bash 单行命令附加或修改 conf 文件中的键

java - Java项目中config.properties文件的位置

c# - XPath C# 属性值

c# - 将 AsyncCompletedEventHandler 设置为类外的方法

c# - 当请求离开 asp :multiview or ajax 时,在 Response.End() 之后没有写入响应

c# - 在服务器应用程序中存储配置信息的最佳方法是什么? (无数据库)

存储设置/缓存的跨平台方式

c# - 如何使用JSON.Net读取JSON并输出HTML?

c# - 如何反转查询或c#中的单词顺序

c# - 依赖注入(inject)——通过配置文件在运行时选择DLL和类实现