问题描述:读取APPconfig文件中的某个节点的的值,利用配置文件中的值初始化某些值
解决办法:利用ConfigurationManager的Appsettings["某个要获取的字段名"]的方法得到配置文件中的值。
private void NormalForm_Load(object sender, EventArgs e)
{
string WorkUnitName = ConfigurationManager.AppSettings["WorkUnitName"].ToString().Trim(); //工位使用
string ProcedureName = ConfigurationManager.AppSettings["ProcedureName"].ToString().Trim();//工序
WorkUnitNO = ConfigurationManager.AppSettings["WorkUnitNO"].ToString().Trim(); //工位编码
ProcedureNO = ConfigurationManager.AppSettings["ProcedureNO"].ToString().Trim();//工序编码
tosslabOperater.Text =" 操作员:"+ OperaterName;
tosslabStationNo.Text = " 当前工作单元: " + WorkUnitName;
tosslabCurrentProcedure.Text = " 当前工序: " + ProcedureName;
mShowChart(); //初次加载工位统计数目
mShowConfig(); //判断是否显示配置按钮
}
学习内容:
1.使用ConfigurationManager对象时必须要引用System.Configuration命名空间(dll),不然会出现无法引用的错误,.NET中提供了System.Configuration.dll,这个命名空间下提供的类可以很方便的把这些参数配置读写到XML文件中。
2.一些与配置文件相关的类:
ConfigurationManager:将当前程序的配置文件作为Configuration对象打开。
ConfigurationSection:表示配置文件中的区域对象。
ConfigurationElement:表示配置文件中的元素对象。
ConfigurationElementCollection:配置文件元素对象的集合
3.可以直接在应用程序中添加一个配置文件,点击项目—添加类—应用程序配置文件—文件名app.config的XML文件。
4.Section区域:它是XML文件的除根节点外的一级子节点,在它的下边还可以其他子节点和元素
Element元素:它是XML文件中的最基本的元素,它的下边不能再有其他的元素。
这两个元素都可以有属性,它们的属性就是在其XML内部的标签。
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="section1" type="OracleDataConvertTxt.AppSectionA, OracleDataConvertTxt, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="SectionA" type="OracleDataConvertTxt.AppSectionB, OracleDataConvertTxt, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="SectionB" type="OracleDataConvertTxt.AppSectionB, OracleDataConvertTxt, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<appSettings>
<add key="huangbo" value="1234567890" />
</appSettings>
<connectionStrings>
<add name="连接的名称,就类同于Key" connectionString="具体的连接字符串" providerName="与连接字符串一起使用的提供程序的名称。这个有时可以没有" />
</connectionStrings>
<section1 KeyName="" KeyValue="">
<AppElement KeyName="this is key" KeyValue="this is value" KeyValue2="this is value2" />
</section1>
<SectionA KeyName="hahahaha" KeyValue="1234567" />
<SectionB KeyName="this is key name" KeyValue="this is key value">
<ElementCollection>
<add KeyName="e1 key" KeyValue="e1 value" KeyValue2="e1 value2" />
<add KeyName="e2 key" KeyValue="e2 value" KeyValue2="e2 value2" />
<add KeyName="e3 key" KeyValue="e3 value" KeyValue2="e3 value2" />
</ElementCollection>
</SectionB>
</configuration>
5.
一般XML文件的结构分析:
1、整个XML配置文件以<?xml version="1.0" encoding="utf-8"?> 为头部声明指示版本为1.0 字符编码为utf-8
2、有一个根节点<configuration>。所有内容都必须在这个根节点中。
3、有一个自定义区域的声明块<configSections>。所有自定义的区域必须在这个块中声明。
4、有二个预定义的区域<appSettings>及<connectionStrings>。它们是.NET预先定义好的区域,如果没有使用到它们那在文件中就不会出现这二个区域。
5、若干其它自定义区域如上面出现的:<section1> ,<SectionA>, <SectionB>。这些区域的标签名必须和自定义区域声明中声明的section name一致。
6、在每个自定义区域中可以有若干子节点,元素集合,与基本元素。
7、所有自定义的区域,集合,都可以有若干属性,如<SectionA KeyName="hahahaha" KeyValue="1234567" /> 中的KeyName="hahahaha" KeyValue="1234567" 就是二个属性。
8、所有在自定义区域下的元素都可以有若干属性,但必须以add标识开头,并且第一个属性为key值,在同一组元素中的key值不能相同。
9、二个预定义区域下的元素属性是固定的,不可以出现除规定属性外其它的属性。
7.定义各个区域集合,和元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace OracleDataConvertTxt
{
//AppSectionA,AppSectionB:区域节点类,在XML中为一个仅次于根节点的子节点。继承自ConfigurationSection。
//AppElementCollection:元素集合类,在XML中为一个区域节点下的子节点。继承自ConfigurationElementCollection。
//LoadSettingsElement:元素类,在XML中为一个基本元素。继承自ConfigurationElement。
//AppSectionGroup:区域节点集合类,在XML中为一组区域。继承自ConfigurationSectionGroupCollection
public sealed class AppSectionA : ConfigurationSection
{//一个简单的AppSectionA区域 只包含二个属性和一个固定元素
//第一个属性
[ConfigurationProperty("KeyName", IsRequired = true)]
public string KeyName
{
get { return (string)base["KeyName"]; }
set { base["KeyName"] = value; }
}
//第二个属性
[ConfigurationProperty("KeyValue", IsRequired = true)]
public string KeyValue
{
get { return (string)base["KeyValue"]; }
set { base["KeyValue"] = value; }
}
//一个元素
[ConfigurationProperty("AppElement", IsRequired = true)]
public AppElement AppElement
{
get { return (AppElement)base["AppElement"]; }
}
}
public sealed class AppSectionB : ConfigurationSection
{//复杂一点AppSectionB区域,包含二个属性和一个AcountElementCollection元素集合(元素集合的好处是可以动态的添加和删除集合中的元素)
//第一个属性
[ConfigurationProperty("KeyName", IsRequired = true)]
public string KeyName
{
get { return (string)base["KeyName"]; }
set { base["KeyName"] = value; }
}
//第二个属性
[ConfigurationProperty("KeyValue", IsRequired = true)]
public string KeyValue
{
get { return (string)base["KeyValue"]; }
set { base["KeyValue"] = value; }
}
//一个元素
[ConfigurationProperty("ElementCollection", IsRequired = true)]
public AppElementCollection ElementCollection
{
get { return (AppElementCollection)base["ElementCollection"]; }
}
}
public sealed class AppElementCollection : ConfigurationElementCollection
{//定义一个AppElementCollection元素集合类
//其实关键就是这二个索引器,但它也是调用基类的实现,只是做下类型转换就行了
//这二个索引器一个是字符串版本,一个是数字版本。由于这些成员被基类声明为了protected,所以要公开这些方法给外部使用。而索引器是不能被继承的,所有要NEW
new public AppElement this[string name]
{
get { return (AppElement)base.BaseGet(name); }
//set 可以不用设置。因为可以通过get返回后,直接修改返回的对象就可以了,它们是引用类型。
set { AppElement app = ((AppElement)BaseGet(name)); app.KeyName = value.KeyName; app.KeyValue = value.KeyValue; app.KeyValue2 = value.KeyValue2; }
}
new public AppElement this[int index]
{
get { return (AppElement)base.BaseGet(index); }
//set 可以不用设置。因为可以通过get返回后,直接修改返回的对象就可以了,它们是引用类型。
set { AppElement app = ((AppElement)BaseGet(index)); app.KeyName = value.KeyName; app.KeyValue = value.KeyValue; app.KeyValue2 = value.KeyValue2; }
}
//下面二个方法中抽象类中必须要实现的
protected override ConfigurationElement CreateNewElement()
{
return new AppElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((AppElement)element).KeyName;
}
//说明:如果不需要在代码中修改集合,可以不实现Add,Clear,Remove
public void Add(AppElement setting)
{
this.BaseAdd(setting);
}
public void Clear()
{
this.BaseClear();
}
public ConfigurationElement Get(string keyName)
{
return this.BaseGet(keyName);
}
public void Remove(string keyName)
{
this.BaseRemove(keyName);
}
}
public sealed class AppElement : ConfigurationElement
{
//这是定义基本的元素类,它有三个属性,默认第一属性为它的Key,Key的值不能重复
[ConfigurationProperty("KeyName", IsRequired = true)]
public string KeyName
{
get { return this["KeyName"].ToString(); }
set { base["KeyName"] = value; }
}
[ConfigurationProperty("KeyValue", IsRequired = false)]
public string KeyValue
{
get { return this["KeyValue"].ToString(); }
set { this["KeyValue"] = value; }
}
[ConfigurationProperty("KeyValue2", IsRequired = false)]
public string KeyValue2
{
get { return this["KeyValue2"].ToString(); }
set { this["KeyValue2"] = value; }
}
}
}
8.在程序中运用:
private void button1_Click(object sender, EventArgs e) { //得到一个配置对象 Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //--------------.NET提供的二个预定义区域 AppSettings 及 ConnectionStrings ----------- //使用预定义的AppSettings区域配置节点,在里面添加元素,元素必须是一个KeyValueConfigurationElement类的对象。 //这个对象是一个键、值对。 KeyValueConfigurationElement Element = new KeyValueConfigurationElement("huangbo", "1234567890"); cfg.AppSettings.Settings.Add(Element); //也可以直接写成 cfg.AppSettings.Settings.Add("huangbo", "1234567890"); //使用预定义的ConnectionStrings区域配置节点,在里面添加元素,元素必须是一个ConnectionStringSettings类的对象 ConnectionStringSettings constrSetting = new ConnectionStringSettings("连接的名称,就类同于Key", "具体的连接字符串", "与连接字符串一起使用的提供程序的名称。这个有时可以没有"); cfg.ConnectionStrings.ConnectionStrings.Add(constrSetting); //-------------以下是自定义区域------------ //声明一个区域section1并把它加入到cfg中。 AppSectionA section1 = new AppSectionA(); section1.AppElement.KeyName = "this is key"; section1.AppElement.KeyValue = "this is value"; section1.AppElement.KeyValue2 = "this is value2"; cfg.Sections.Add("section1", section1); //声明一个区域sectionA并把它加入到cfg中。 AppSectionB sectionA = new AppSectionB(); sectionA.KeyName = "hahahaha"; sectionA.KeyValue = "1234567"; cfg.Sections.Add("SectionA", sectionA); //声明一个区域sectionB并把它加入到cfg中。 AppSectionB sectionB = new AppSectionB(); sectionB.KeyName = "this is key name"; sectionB.KeyValue = "this is key value"; AppElement e1 = new AppElement(); AppElement e2 = new AppElement(); AppElement e3 = new AppElement(); e1.KeyName = "e1 key"; e1.KeyValue = "e1 value"; e1.KeyValue2 = "e1 value2"; e2.KeyName = "e2 key"; e2.KeyValue = "e2 value"; e2.KeyValue2 = "e2 value2"; e3.KeyName = "e3 key"; e3.KeyValue = "e3 value"; e3.KeyValue2 = "e3 value2"; sectionB.ElementCollection.Add(e1); sectionB.ElementCollection.Add(e2); sectionB.ElementCollection.Add(e3); cfg.Sections.Add("SectionB", sectionB); //删除一个元素 删除sectionB下面ElementCollection元素集合里的第一个Key值叫 e1 key 的元素。(Key值就是元素的第一个属性值) //必须是ElementCollection下的元素才允许被删除,因为ElementCollection是一个集合,集合里的项是可以被删除的。而直接在区域下的元素无法删除,因为它被定义成类的属性了。 //sectionB.ElementCollection.Remove("e1 key"); //删除一个区域SectionA //cfg.Sections.Remove("SectionA"); //更新一个元素 //sectionB.ElementCollection["e2 key"].KeyValue = "e2 new value"; //将Configuration对象cfg的配置 写入到当前XML文件中去 cfg.Save(); //刷新命名节点,这样在下次检索它时将重新从磁盘读取它。 ConfigurationManager.RefreshSection("sectionB"); //直接给赋值 //AppElement tmpelement = new AppElement(); //tmpelement.KeyName = "bob"; //tmpelement.KeyValue = "bobvalue"; //tmpelement.KeyValue2 = "bobvalue2"; //AppSectionB appsection2 = cfg.GetSection("SectionB") as AppSectionB; //appsection2.ElementCollection["e3 key"] = tmpelement; //cfg.Save(); }