System.Konfiguration mit Separaten .config-Datei

Ich habe den code aus diesem Beispiel.

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

Was ich mich Frage (und ich habe alle über das internet heute suchen) ...
Wie bekommt man die in einer externen(separate Datei).

Die Idee werde ich für ist:

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


  <pageAppearanceGroup fileName="SomeSeparateFile.config"/>

</configuration>

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

Die oben nicht funktioniert (natürlich).

Die unten ist meine kopieren/einfügen der ms-Artikel habe ich oben erwähnt.
Und es war voll funktionieren, wenn ich klebte es hier.

//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>
  • Zum einbinden von code-und/oder XML auf dieser Website, wählen Sie es im editor und drücken Sie Strg-K.
  • BTW, hast du irgendwelche änderungen an der code eingefügt?
  • Keine änderungen. Das einzige, was ich getan habe war setzen Sie ein Leerzeichen nach dem < in die xml, und ein Leerzeichen vor dem > in die xml. Aber der C# - code kopieren und einfügen, und ich habe die "code" - tagger Sache in das textblock-Steuerelement.
  • Du bist erroring, denn sollten Sie extrahieren configSections-Dateien zu trennen, nicht configSectionGroups. Bewegen configSections in separaten Dateien wird unterstützt durch die Konfigurations-framework. Scheint nicht, dass sich die Gruppen sind. Siehe meine änderungen zu Ihrer config-Datei unten. Getestet habe ich Sie in einer Beispiel-app und es funktionierte gut.
InformationsquelleAutor granadaCoder | 2010-07-29
Schreibe einen Kommentar