C#中4种方式读取配置项
来源:互联网 发布:网络骗女性的骗术大全 编辑:程序博客网 时间:2024/06/05 09:36
方法1:
Let’s take one example, where we need to add some application level settings and access them based on their keys. We can add these setting either inside Web.Config or App.Config. But we need to add <appSettings> section inside the configuration section.
Just follow the following example, where inside the appSettings section; we have defined few keys and their values.
App.config
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="Title" value="Configuration Example"/> <add key="Language" value="CSharp"/> </appSettings></configuration>
To access these values, there is one static class as named “ConfigurationManager” which has one getter property as named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section as following.
public static void GetConfigurationValue(){ var title = ConfigurationManager.AppSettings["title"]; var language = ConfigurationManager.AppSettings["language"]; Console.WriteLine(string.Format("'{0}' project is created in '{1}' language ", title, language));}
When we implement the above code, we get following out.
方法2:
In following example, we have just created one custom section as named “ApplicationSettings” and added all key/value pairs separately.
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="ApplicationSettings" type="System.Configuration.NameValueSectionHandler"/> </configSections> <ApplicationSettings> <add key="ApplicationName" value="Configuration Example Project"/> <add key="Language" value="CSharp"/> <add key="SecretKey" value="012345"/> </ApplicationSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup></configuration>
To access custom section settings, first we need to find out the section using GetSection method which is defined inside the ConfigurationManager class and cast the return value as NameValueCollection. It will return all the keys available inside this custom section and based on keys we can get values easily as following.
public static void GetConfigurationUsingSection(){ var applicationSettings = ConfigurationManager.GetSection("ApplicationSettings") as NameValueCollection; if (applicationSettings.Count == 0) { Console.WriteLine("Application Settings are not defined"); } else { foreach (var key in applicationSettings.AllKeys) { Console.WriteLine(key + " = " + applicationSettings[key]); } }}
When we implement the above code, we get following out.
方法3:
Now move to some tough stuff, here we are going to create section inside the group, so that if required we can add multiple sections in same group. It is basically grouping the same type of section in a group.
In following example, we have created one group as named “BlogGroup” and inside that we have defined one section as named “PostSetting” and its type as a NameValueSectionHandler. “PostSetting” section is containing all the key/value pair separately as following.
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <sectionGroup name="BlogGroup"> <section name="PostSetting" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> <section name="ProductSettings" type="ConfigurationExample.ProductSettings, ConfigurationExample"/> </configSections> <BlogGroup> <PostSetting> <add key="PostName" value="Getting Started With Config Section in .Net"/> <add key="Category" value="C#"></add> <add key="Author" value="Mukesh Kumar"></add> <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.5.2"/> </startup></configuration>
To read these types of configuration setting, we need to access section based on section group and then we can get all the keys and their values as following code is doing.
public static void GetConfigurationUsingSectionGroup(){ var PostSetting = ConfigurationManager.GetSection("BlogGroup/PostSetting") as NameValueCollection; if (PostSetting.Count == 0) { Console.WriteLine("Post Settings are not defined"); } else { foreach (var key in PostSetting.AllKeys) { Console.WriteLine(key + " = " + PostSetting[key]); } }}
When we implement the above code, we get following out.
方法4:
At last we are on advance stage of configuration settings. Sometimes it is required to setup your all key/value pair based on custom class behavior so that we can control it behavior form outer world.
See the following class “DellFeatures”, which shows some custom properties of Dell laptop and we need to add it inside the configuration section. Following class contains some default values if value is not available in configuration section.
using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConfigurationExample{ public class DellFeatures : ConfigurationElement { [ConfigurationProperty("ProductNumber", DefaultValue = 00000, IsRequired = true)] public int ProductNumber { get { return (int)this["ProductNumber"]; } set { value = (int)this["ProductNumber"]; } } [ConfigurationProperty("ProductName", DefaultValue = "DELL", IsRequired = true)] public string ProductName { get { return (string)this["ProductName"]; } set { value = (string)this["ProductName"]; } } [ConfigurationProperty("Color", IsRequired = false)] public string Color { get { return (string)this["Color"]; } set { value = (string)this["Color"]; } } [ConfigurationProperty("Warranty", DefaultValue = "1 Years", IsRequired = false)] public string Warranty { get { return (string)this["Warranty"]; } set { value = (string)this["Warranty"]; } } }}
To return this setting, we are going to create on more class which returns this as a property. Here we can also add multiple classes as properties.
using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConfigurationExample{ public class ProductSettings : ConfigurationSection { [ConfigurationProperty("DellSettings")] public DellFeatures DellFeatures { get { return (DellFeatures)this["DellSettings"]; } set { value = (DellFeatures)this["DellSettings"]; } } }}
To implement it inside the configuration section, we are going to change the type of “ProductSettings” as “ConfigurationExample.ProductSettings” which will return all the property of DellFeaturs class.
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <section name="ProductSettings" type="ConfigurationExample.ProductSettings, ConfigurationExample"/> </configSections> <BlogGroup> <PostSetting> <add key="PostName" value="Getting Started With Config Section in .Net"/> <add key="Category" value="C#"></add> <add key="Author" value="Mukesh Kumar"></add> <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.5.2"/> </startup></configuration>
To access this type of configuration, same we need to get custom section first and rest of will be accessible very easily as following code.
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 Color = " + color); Console.WriteLine("Product Warranty = " + warranty); }}
When we implement the above code, we get following out.
- C#中4种方式读取配置项
- C#中读取应用程序配置信息
- C#读取XML方式
- c# 中关于DataReader读取数据的方式
- 四种读取配置信息的方式
- 读取配置信息方式
- C#配置项的保存与读取
- C# 配置信息读取
- C# 读取ini配置 :
- VS中C#读取app.config数据库配置字符串的三种方法
- VS中C#读取app.config数据库配置字符串的三种方法(转)
- VS2008,C#的WinForm中读取App.config配置
- C# Dom方式读取 xml
- C#读取MAC地址方式
- C#读取excel--OLEDB方式
- C#读取XML DOM方式读取
- xml 4种读取方式
- C#读取Excel数据两种方式性能对比
- ORACLE IMP-00058: 遇到 ORACLE 错误 1691 增加表空间
- 关于苹果健康数据的获取
- RecycleView设置点击的两种方式:adapter中的onBindViewHolder(VH,int)方法中进行;实现RecyclerView的addonItemTouchListerner()
- Java 中 转换 CST 时间为 Date 对象
- iOS网络层架构设计分享
- C#中4种方式读取配置项
- 关于解析SM2证书
- 由于Intel OPA驱动导致安装MLNX_OFED驱动后无法启动服务
- Oracle-测试创建反向索引及表分区和索引分区
- 理工 蛇形填数
- 1. Two Sum
- linux 上使用ftp上传文件
- 快速幂取模(分治思想)
- 设计模式之装饰者模式