Cmab使用方法
1. 新建一个WINform项目 testa 。
2. 引用 Microsoft.ApplicationBlocks.ConfigurationManagement.dll 和 Microsoft.ApplicationBlocks.ConfigurationManagement.Interfaces.dll 两个类库。
3. 在程序里面导入命名空间 using Microsoft.ApplicationBlocks.ConfigurationManagement;
4. 在解决方案管理器里的项目上单击鼠标右键,“ 添加-》添加新项-》应用程序配置文件 ”,把新添加的配置文件命名为 App.Config 单击打开添加配置文件。
当添加完成后就在配置文件中添加如下代码:
1<configuration>
2<configsections>
3<section name="applicationConfigurationManagement" type="Microsoft.ApplicationBlocks.ConfigurationManagement.ConfigurationManagerSectionHandler,Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"></section>
4<section name="OtherConfigFile" type="testa.CustomSectionHandler,testa, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"></section>
5</configsections>
6<applicationconfigurationmanagement defaultsection="UnencryptedXml">
7<configsection name="OtherConfigFile">
8<configcache enabled="true" refresh="1 * * * *"></configcache>
9<configprovider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" encrypted="false" path="../../otherConfigFile.config" refreshonchange="true" signed="false" type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage"></configprovider>
10<protectionprovider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" hashkey="MyXuEd6f+go=" initializationvector="ou95G2/WziI=" symmetrickey="VToaqZjp8C27V90oSmT/CF+afvRGClc9" type="Microsoft.ApplicationBlocks.ConfigurationManagement.DataProtection.BCLDataProtection"></protectionprovider>
11</configsection>
12</applicationconfigurationmanagement>
13</configuration>
5. 添加完配置文件后在程序中再添加一个新的XML配置文件,文件名为OtherConfigFile,然后在此文件中添加如下代码。
1<configuration>
2<otherconfigfile>
3<customconfigurationdata xmlns:xsd=" http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
4<name>tiger</name>
5<age>30</age>
6</customconfigurationdata>
7</otherconfigfile>
8</configuration>
此段XML配置文件是自定义的配置文件其中 configuration、OtherConfigFile、CustomConfigurationData 是固定的XML标识。
包括在 CustomConfigurationData 里面的 name 、 age 两个元素为自定义元素。
6. 新建一个名为 CustomConfigurationData.cs 的类,此类为序列化的类
在类中定义以下两个属性,这两个属性分别为姓名和年龄
public string name
{
get{ return _name; }
set{ _name = value; }
} string _name;
public string age;
{
get{ return _age; }
set{ _age = value; }
} string _age;
7. 新建一个名为 CustomSectionHandler.cs 的类,此类是用来控制读取和写入配置文件的,它实现了 IconfigurationSectionHandler 和 IconfigurationSectionHandlerWriter 两个接口。里面提供的 CREATE 方法是用来读取配置文件数据的, Serialize 是用来写入配置文件数据的。(这两个类加载到程序的命名空间下面)
- [ComVisible(false)]
- public class CustomSectionHandler
- IConfigurationSectionHandler, IConfigurationSectionHandlerWriter
{
XmlSerializer xs = new XmlSerializer( typeof(CustomConfigurationData) );
public object Create( object parent, object hmm, XmlNode configSection )
{
object tmpObj = null;
tmpObj = xs.Deserialize( new StringReader( configSection.OuterXml ) );
return (CustomConfigurationData)tmpObj;
}
public XmlNode Serialize( object value )
{
try
{
StringWriter sw = new StringWriter( System.Globalization.CultureInfo.CurrentUICulture );
xs.Serialize( sw, value );
XmlDocument doc = new XmlDocument();
doc.LoadXml( sw.ToString() );
return doc.ChildNodes[1];
}
catch( Exception e )
{
throw new ConfigurationException( "此配置项不能被序列化!", e );
}
}
}
8. 在窗体上添加两个文本框、两个lable和两个按钮
在两个lable的text上配别填写“姓名”和“年龄”
两个文本框分别命名为 txtName 和 txtAge 。
把 button1 的 name 改为 btnRead , text 属性改为“ 读取 ”, button2 的 name 属性改为 btnWrite ,
text 属性改为“ 写入 ”。
9. 在 btnRead 的单击事件里面添加如下代码
CustomConfigurationData cclass = (CustomConfigurationData)ConfigurationManager.Read("OtherConfigFile" );
txtName.Text = cclass.name;
txtAge.Text = cclass.age;
10.在 btnWrite 的单击事件里添加如下代码
CustomConfigurationData cf = new CustomConfigurationData();
cf.name = txtName.Text.Trim();
cf.age = txtAge.Text.Trim();
ConfigurationManager.Write("OtherConfigFile",cf);
完成上述步骤后运行程序就可以对配置文件进行读写操作了。
如果把配置文件的 encrypted 设置为true的话,就可以实现加密了
1<configuration>
2<configsections>
3<section name="applicationConfigurationManagement" type="Microsoft.ApplicationBlocks.ConfigurationManagement.ConfigurationManagerSectionHandler,Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"></section>
4<section name="OtherConfigFile" type="testa.CustomSectionHandler,testa, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"></section>
5</configsections>
6<applicationconfigurationmanagement defaultsection="UnencryptedXml">
7<configsection name="OtherConfigFile">
8<configcache enabled="true" refresh="1 * * * *"></configcache>
9<configprovider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" encrypted="true" path="../../otherConfigFile.config" refreshonchange="true" signed="false" type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage"></configprovider>
10<protectionprovider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" hashkey="MyXuEd6f+go=" initializationvector="ou95G2/WziI=" symmetrickey="VToaqZjp8C27V90oSmT/CF+afvRGClc9" type="Microsoft.ApplicationBlocks.ConfigurationManagement.DataProtection.BCLDataProtection"></protectionprovider>
11</configsection>
12</applicationconfigurationmanagement>
13</configuration>
5. 添加完配置文件后在程序中再添加一个新的XML配置文件,文件名为OtherConfigFile,然后在此文件中添加如下代码。
1<configuration>
2<otherconfigfile>
3<customconfigurationdata xmlns:xsd=" http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
4<name>tiger</name>
5<age>30</age>
6</customconfigurationdata>
7</otherconfigfile>
8</configuration>