views:

76

answers:

2

I took the code from this example.

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. 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>
+1  A: 

Try using the configSource

http://theburningmonk.com/2010/03/dot-net-tips-using-configsource-or-file-attribute-to-externalize-config-sections/

http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx

theburningmonk
There’s a lit­tle known attribute which was intro­duced in the .Net frame­work 2.0 called con­fig­Source, which allows you to exter­nal­ize sec­tions of the con­fig­u­ra­tion file. <appSet­tings file = “rel­a­tive file name” />but can be added to any con­fig­u­ra­tion sec­tion to spec­ify an exter­nal file for that section: <Cus­tom­Sec­tion configSource=“relative file name” />The above is a quote from the first URL. (In case it stops existing in the future).// lit­tle known attribute // End QuoteNow I don't feel so bad. THANKS!
granadaCoder
+1  A: 

This will work if you change your app.config to use this:

<?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:

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

(no configuration element in this file!)

I've been able to move configSections into separate files. Not sure you can do that with configGroups unless you do a lot more programming. The configuration framework model lets you move configSections out pretty easily.

Hopefully this helps!!

David Hoerster
//Quote//Not sure you can do that with configGroups unless you do a lot more programming.//End QuoteI was simply staying "true" to the original msdn example on that one.(I think) the extra configGroups thing is more trouble than it is worth.Thanks....
granadaCoder