My Development Notes

By Haemoglobin
3/10/2010 (revision 1)

Configuration

 //Machine.config
 <section name="authentication" type="System.Web.Configuration.AuthenticationConfigHandler" allowDefinition="MachineToApplication"/"MachineOnly">

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
 config.AppSettings.Settings.Add("Key", "Value"); 

 ConfigurationManager.AppSettings[keyName]; 

 ConnectionStringSettings conn = ConfigurationManager.ConnectionSTrings["sql"]; 
 conn.ConnectionString
 conn.ProviderName //Switch on this to determine whether to create new SqlConnection(), new OracleConnection()

 ConfigurationManager.OpenMachineConfiguration().GetSection(sectionName).DefaultProvider

 public class MySettings {
    public string lastName; 
    public int lastNumber;
 }

 public class ConfigHandler : IConfigurationSectionHandler {
    public object Create(object parent, object configContext, System.Xml.XmlNode section) {
        settings = new MySettings(); 
        settings.lastName = section.SelectSingleNode("lastName").InnerText; 
        return settings;
    }
 }

 MySettings settings = (MySettings)ConfigurationManager.GetSection(sectionName); 

 <configuration>
    <configSections>

        <section name="customSettings" type="CustomConfigHandler, ConfigApp"/>
    </configSections>

    <customSettings> 
        <lastName>Graham</lastName>
        <Manager name="John Smith"/>
    </customSettings>

 </configuration> 

 //PREFERRED
 public class MySettings : ConfigurationSection
    [ConfigurationProperty("lastName", DefaultValue = "Graham", IsRequired = true)]
    [StringValidator(InvalidCharacters = "!@#$", MinLength=2)]
    public string lastName {
        get { return this["lastName"]; } 
        set { this["lastName"] = value; }
    }

    [ConfigurationProperty("Manager")]
    public ManagerElement Manager {  //ManagerElement extends ConfigurationElement
        get { return (ManagerElement) this["ManagerElement"]; } 
    }
 }

 configuration
 |__startup
    |__supportedRuntime
 |__runtime
    |__<developmentMode developerInstallation="true"/> //Will search %DEVPATH% environment variable for assemblies if exists.
    |__assemblyBinding
        |__assemblyIdentity
            |__codeBase
        |__bindingRedirect

    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>

            <assemblyIdentity name="myAssembly"
              publicKeyToken="32ab4ba45e0a69a1"
              culture="en-us" />
              <codeBase version="2.0.0.0" href="http://www.litwareinc.com/myAssembly.dll"/>
            <!-- Assembly versions can be redirected in application, 
              publisher policy, or machine configuration files. -->
            <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>

          <assemblyIdentity name="mySecondAssembly"
            publicKeyToken="32ab4ba45e0a69a1"
            culture="en-us" />
            <!-- Publisher policy can be set only in the application 
              configuration file. -->
            <publisherPolicy apply="no" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>

    </configuration>


    <configuration>
       <startup>
          <supportedRuntime version="v1.1.4322"/>
          <supportedRuntime version="v1.0.3705"/>

       </startup>
    </configuration>


 

Comments

Powered by BlogEngine.NET 1.6.1.0 | Design by styleshout | Enhanced by GravityCube.net | 1.4.5 Changes by zembian.com | Adapted by HamishGraham.NET
(c) 2010 Hamish Graham. Banner Image (c) Chris Gin