Friday, December 13, 2024

An example of .net Configuration

 App.config:


<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="registrations" type="MyApp.MyConfigurationSection, MyApp" />
        <section name="vssPathSettings" type="System.Configuration.DictionarySectionHandler" />
    </configSections>
    <registrations myValue="Hello World" />
  <vssPathSettings>
    <add key="Form.Script" value="Obj|$/FormControl/SyteLineDev/Forms/" />
    <add key="SQL_STORED_PROCEDURE" value="App|$/ApplicationDB/Stored Procedures/" />
  </vssPathSettings> 
  <ConnectionManagerDataSection>
    <ConnectionManagerEndpoints>
      <add name="Development" address="Dev.MyDomain.local" useSSL="false" />
      <add name="Test" address="Test.MyDomain.local" useSSL="true" />
      <add name="Live" address="Prod.MyDomain.com" useSSL="true" securityGroupsAllowedToSaveChanges="ConnectionManagerUsers" />
    </ConnectionManagerEndpoints>
  </ConnectionManagerDataSection>
</configuration>

using System;
using System.Configuration;
namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = ConfigurationManager.GetSection(MyConfigurationSection.SectionName) as MyConfigurationSection ?? new MyConfigurationSection();

            Console.WriteLine(s.MyValue);

            s = new MyConfigurationSection();
            s.MyValue = "I am modified.";
            Save(s);

            s = ConfigurationManager.GetSection(MyConfigurationSection.SectionName) as MyConfigurationSection ?? new MyConfigurationSection();

            Console.WriteLine(s.MyValue);

            var section = ConfigurationManager.GetSection(name) as IDictionary;
            Console.WriteLine(
section["key"]
);

            var connectionManagerDataSection = ConfigurationManager.GetSection(ConnectionManagerDataSection.SectionName) as ConnectionManagerDataSection;
            if (connectionManagerDataSection != null)
            {
                foreach (ConnectionManagerEndpointElement endpointElement in connectionManagerDataSection.ConnectionManagerEndpoints)
                {
                    string Name = endpointElement.Name;
                    Console.WriteLine(Name);
                    string Address = endpointElement.Address;
                    Console.WriteLine(Address);
                    bool UseSSL = endpointElement.UseSSL;
                    Console.WriteLine(UseSSL);
                    List<string> SecurityGroupsAllowedToSaveChanges = endpointElement.SecurityGroupsAllowedToSaveChanges.
                        Split(',').Where(e => !string.IsNullOrWhiteSpace(e)).ToList();
                    foreach(string v in SecurityGroupsAllowedToSaveChanges)
                    {
                        Console.WriteLine(v);
                    }              
                }
            }

            Console.ReadLine();
        }

        private static void Save(MyConfigurationSection s)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.Sections[MyConfigurationSection.SectionName] != null)
            {
                config.Sections.Remove(MyConfigurationSection.SectionName);
            }

            config.Sections.Add(MyConfigurationSection.SectionName, s);
            s.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection(MyConfigurationSection.SectionName);
        }
    }

    public class MyConfigurationSection : ConfigurationSection
    {
        public const String SectionName = "registrations";

        [ConfigurationProperty("myValue")]
        public String MyValue
        {
            get { return (String)this["myValue"]; }
            set { this["myValue"] = value; }
        }

    }
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApp
{
    public class ConnectionManagerDataSection : ConfigurationSection
    {
        /// <summary
        /// The name of this section in the app.config.
        /// </summary>
        public const string SectionName = "ConnectionManagerDataSection";

        private const string EndpointCollectionName = "ConnectionManagerEndpoints";

        [ConfigurationProperty(EndpointCollectionName)]
        [ConfigurationCollection(typeof(ConnectionManagerEndpointsCollection), AddItemName = "add")]
        public ConnectionManagerEndpointsCollection ConnectionManagerEndpoints
        {
            get { return (ConnectionManagerEndpointsCollection)base[EndpointCollectionName]; }
        }
    }

    public class ConnectionManagerEndpointsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ConnectionManagerEndpointElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ConnectionManagerEndpointElement)element).Name;
        }
    }

    public class ConnectionManagerEndpointElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("address", IsRequired = true)]
        public string Address
        {
            get { return (string)this["address"]; }
            set { this["address"] = value; }
        }

        [ConfigurationProperty("useSSL", IsRequired = false, DefaultValue = false)]
        public bool UseSSL
        {
            get { return (bool)this["useSSL"]; }
            set { this["useSSL"] = value; }
        }

        [ConfigurationProperty("securityGroupsAllowedToSaveChanges", IsRequired = false)]
        public string SecurityGroupsAllowedToSaveChanges
        {
            get { return (string)this["securityGroupsAllowedToSaveChanges"]; }
            set { this["securityGroupsAllowedToSaveChanges"] = value; }
        }
    }
}

No comments:

Post a Comment