February 12, 2011 by Christoff Truter C#
Generally when we develop applications using .NET, we use our app/web.config as a place
where we expose certain settings to our application(s) (its even possible to
encrypt
it for those who dare or feel the higher calling to use it to store sensitive information).
Your config file will likely look something like this:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="1" value="a" /> <add key="2" value="b" /> <add key="3" value="c" /> </appSettings> </configuration>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="Company.appSettings" type="System.Configuration.NameValueFileSectionHandler" /> </configSections> <Company.appSettings> <add key="1" value="a" /> <add key="2" value="b" /> <add key="3" value="c" /> </Company.appSettings> <appSettings /> </configuration>
NameValueCollection settings = ConfigurationManager.GetSection("Company.appSettings") as NameValueCollection; if (settings != null) { foreach (string key in settings) { Console.WriteLine("{0} : {1}", key, settings[key]); } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="Company"> <section name="appSettings" type="System.Configuration.NameValueFileSectionHandler" /> </sectionGroup> </configSections> <Company> <appSettings> <add key="1" value="a" /> <add key="2" value="b" /> <add key="3" value="c" /> </appSettings> </Company> <appSettings /> </configuration>
NameValueCollection settings = ConfigurationManager.GetSection("Company/appSettings") as NameValueCollection;
<system.net> <mailSettings> <smtp> <network host="smtp.somehost.net" /> </smtp> </mailSettings> </system.net>
using System; using System.Configuration; namespace Sections { public class XSection : ConfigurationSection { [ConfigurationProperty("x", DefaultValue = false, IsRequired = false)] public Boolean X { get { return (Boolean)this["x"]; } set { this["x"] = value; } } [ConfigurationProperty("y")] public YElement Y { get { return (YElement)this["y"]; } set { this["y"] = value; } } } public class YElement : ConfigurationElement { [ConfigurationProperty("y1", DefaultValue = "value", IsRequired = false)] public String Y1 { get { return (String)this["y1"]; } set { this["y1"] = value; } } [ConfigurationProperty("y2", IsRequired = true)] public Int32 Y2 { get { return (Int32)this["y2"]; } set { this["y2"] = value; } } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="XSection" type="Sections.XSection, ConsoleApplication1" allowLocation="true" allowDefinition="Everywhere" /> </configSections> <XSection x="true"> <y y1="test1" y2="10" /> </XSection> </configuration>
Sections.XSection xsection = ConfigurationManager.GetSection("XSection") as Sections.XSection; if (xsection != null) { Console.WriteLine(xsection.X); Console.WriteLine(xsection.Y.Y1); Console.WriteLine(xsection.Y.Y2); }
Re: Useful but.. February 20, 2014 by Christoff Truter
Thanks Rex, wish you elaborated on what you find so vague? This is a very basic concept though?