Vb Net App Config Read for Each Appsettings
This commodity will demonstrate how we tin become/read the configuration setting from Spider web.Config or App.Config in C#. At that place are different ways to ready the values inside the configuration file and read their values, which are based on the defined keys. We define those values inside the configuration section, which might be needed to make information technology more than secure. Information technology can be some secret keys or the value, which should be received frequently.
Today, I will bear witness you lot four dissimilar ways to get the values from the configuration section.
For this demonstration, I am going to create a simple panel Application and provide the proper noun equally "ConfigurationExample". Just create one console Application, as shown below.
New Project > Visual C# > Console Application
We demand to add System.Configuration assembly reference to access configuration settings, using ConfigurationManager. To add together the reference, just right click References and click to add together references.
Now, we tin see that System.Configuration reference has been added successfully to our project.
Thus, let's move to unlike ways to add the values inside the config file and the approach nosotros follow to get it.
Offset approach
Let's take one example, where we demand to add some Application level settings and admission them based on their keys. We can add these settings either inside Spider web.Config or App.Config but nosotros need to add <appSettings> department inside the configuration section.
Just follow the example given below, where inside the appSettings section; we take divers few keys and their values.
App.config
<?xml version="1.0" encoding="utf-eight" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add primal="Title" value="Configuration Example"/> <add primal="Language" value="CSharp"/> </appSettings> </configuration>
To admission these values, in that location is i static class named ConfigurationManager, which has one getter belongings named AppSettings. We can just pass the primal inside the AppSettings and get the desired value from AppSettings department, equally shown below.
public static void GetConfigurationValue() { var title = ConfigurationManager.AppSettings["title"]; var language = ConfigurationManager.AppSettings["language"]; Console.WriteLine(string.Format("'{0}' projection is created in '{1}' language ", title, language)); }
When we implement the code given above, we become the output, as shown below.
Second approach
Let's move to the next instance. If we need to add the settings inside the department for the separation, in this situation, we can create a custom department inside the configuration department in App.Config/Spider web.Config, as shown below. This section can brand your data more readable and understandable based on your section name.
In the example given below, we have just created one custom section named ApplicationSettings and added all the key/value pairs separately.
<?xml version="1.0" encoding="utf-viii" ?> <configuration> <configSections> <section name="ApplicationSettings" blazon="Arrangement.Configuration.NameValueSectionHandler"/> </configSections> <ApplicationSettings> <add key="ApplicationName" value="Configuration Example Project"/> <add key="Linguistic communication" value="CSharp"/> <add key="SecretKey" value="012345"/> </ApplicationSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
To admission custom section settings, we first need to find out the section, using GetSection method, which is defined inside the ConfigurationManager class and cast the return value as NameValueCollection. Information technology volition render all the keys bachelor within this custom section and based on the keys, we can get the values easily, as shown beneath.
//Arroyo Two public static void GetConfigurationUsingSection() { var applicationSettings = ConfigurationManager.GetSection("ApplicationSettings") as NameValueCollection; if (applicationSettings.Count == 0) { Panel.WriteLine("Awarding Settings are not divers"); } else { foreach (var key in applicationSettings.AllKeys) { Console.WriteLine(cardinal + " = " + applicationSettings[central]); } } }
When we implement the code given abbove, we become the output given below.
Tertiary approach
Now, let'south move to some tough stuff. Here, nosotros are going to create a department inside the grouping, so that, if required, we can add together multiple sections in the same grouping. Information technology is basically grouping the same type of section in a group.
In the example given below, nosotros have created one group named BlogGroup and inside it, we take defined ane section, named it "PostSetting" and its blazon as a NameValueSectionHandler. "PostSetting" section contains all the key/value pair separately, as shown beneath.
<?xml version="i.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup proper name="BlogGroup"> <department proper noun="PostSetting" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> <section name="ProductSettings" type="ConfigurationExample.ProductSettings, ConfigurationExample"/> </configSections> <BlogGroup> <PostSetting> <add key="PostName" value="Getting Started With Config Department in .Net"/> <add together fundamental="Category" value="C#"></add> <add key="Author" value="Mukesh Kumar"></add> <add together key="PostedDate" value="28 Feb 2017"></add> </PostSetting> </BlogGroup> <ProductSettings> <DellSettings ProductNumber="20001" ProductName="Dell Inspiron" Color="Blackness" Warranty="2 Years" ></DellSettings> </ProductSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.v.2"/> </startup> </configuration>
To read these types of configuration settings, we need to access the section. Based on the department group, we tin get all the keys and their values, equally shown below.
//Approach Three public static void GetConfigurationUsingSectionGroup() { var PostSetting = ConfigurationManager.GetSection("BlogGroup/PostSetting") as NameValueCollection; if (PostSetting.Count == 0) { Console.WriteLine("Mail Settings are not defined"); } else { foreach (var central in PostSetting.AllKeys) { Panel.WriteLine(cardinal + " = " + PostSetting[primal]); } } }
When we implement the code given above, we get the output, every bit shown below.
Fourth approach
At concluding, nosotros are on an advanced phase of the configuration settings. Sometimes, it is required to set up your all cardinal/value pairs based on the custom class behavior, so that nosotros can command the behavior from outside.
Run into the following class "DellFeatures", which shows some custom properties of Dell laptops and we demand to add it inside the configuration department. The following class contains some default values, if the value is non available in the configuration department.
using System; using Organisation.Collections.Generic; using Arrangement.Configuration; using System.Linq; using System.Text; using Arrangement.Threading.Tasks; namespace ConfigurationExample { public class DellFeatures : ConfigurationElement { [ConfigurationProperty("ProductNumber", DefaultValue = 00000, IsRequired = true)] public int ProductNumber { go { return (int)this["ProductNumber"]; } set up { value = (int)this["ProductNumber"]; } } [ConfigurationProperty("ProductName", DefaultValue = "DELL", IsRequired = true)] public string ProductName { go { return (string)this["ProductName"]; } set { value = (cord)this["ProductName"]; } } [ConfigurationProperty("Colour", IsRequired = faux)] public string Color { become { render (cord)this["Color"]; } ready { value = (string)this["Colour"]; } } [ConfigurationProperty("Warranty", DefaultValue = "1 Years", IsRequired = false)] public string Warranty { get { return (string)this["Warranty"]; } set { value = (string)this["Warranty"]; } } } }
To return this setting, nosotros are going to create one more than class, which returns this as a property. Here, we can also add multiple classes every bit the properties.
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using Arrangement.Text; using Organisation.Threading.Tasks; namespace ConfigurationExample { public form ProductSettings : ConfigurationSection { [ConfigurationProperty("DellSettings")] public DellFeatures DellFeatures { get { return (DellFeatures)this["DellSettings"]; } set { value = (DellFeatures)this["DellSettings"]; } } } }
To implement it within the configuration section, nosotros are going to change the type of "ProductSettings" as "ConfigurationExample.ProductSettings", which volition return all the properties of DellFeatures course.
<?xml version="ane.0" encoding="utf-8"?> <configuration> <configSections> <section proper noun="ProductSettings" type="ConfigurationExample.ProductSettings, ConfigurationExample"/> </configSections> <BlogGroup> <PostSetting> <add cardinal="PostName" value="Getting Started With Config Department in .Net"/> <add key="Category" value="C#"></add> <add key="Author" value="Mukesh Kumar"></add together> <add key="PostedDate" value="28 Feb 2017"></add> </PostSetting> </BlogGroup> <ProductSettings> <DellSettings ProductNumber="20001" ProductName="Dell Inspiron" Color="Black" Warranty="2 Years" ></DellSettings> </ProductSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.five.2"/> </startup> </configuration>
To access this type of configuration, we need to get the custom section first and the remainder of it volition be accessible very easily, as shown below.
//Approach Iv public static void GetConfigurationUsingCustomClass() { var productSettings = ConfigurationManager.GetSection("ProductSettings") as ConfigurationExample.ProductSettings; if (productSettings == null) { Console.WriteLine("Product Settings are not defined"); } else { var productNumber = productSettings.DellFeatures.ProductNumber; var productName = productSettings.DellFeatures.ProductName; var color = productSettings.DellFeatures.Color; var warranty = productSettings.DellFeatures.Warranty; Console.WriteLine("Product Number = " + productNumber); Console.WriteLine("Product Name = " + productName); Console.WriteLine("Product Colour = " + color); Console.WriteLine("Product Warranty = " + warranty); } }
When nosotros implement the code given higher up, we get the output, every bit shown below.
We have seen different means to define the configuration setting within the configuration file and access/read information technology.
I hope this post helps you. Please give your feedback, which will help me to improve the next mail service. If you have any doubts, please ask your query in the annotate section.
Source: https://www.c-sharpcorner.com/article/four-ways-to-read-configuration-setting-in-c-sharp/
Post a Comment for "Vb Net App Config Read for Each Appsettings"