首页 > C和C# > Configuration Management Application Block 使用方法

Configuration Management Application Block 使用方法

2007年6月17日 发表评论 阅读评论

转自http://blog.csdn.net/qqws/

1. 新建一个WINform项目 testa
2. 引用Microsoft.ApplicationBlocks.ConfigurationManagement.dllMicrosoft.ApplicationBlocks.ConfigurationManagement.Interfaces.dll两个类库。
3. 在程序里面导入命名空间 using Microsoft.ApplicationBlocks.ConfigurationManagement;4. 在解决方案管理器里的项目上单击鼠标右键,“添加-》添加新项-》应用程序配置文件”,把新添加的配置文件命名为App.Config 单击打开添加配置文件。
当添加完成后就在配置文件中添加如下代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="applicationConfigurationManagement" type="Microsoft.ApplicationBlocks.ConfigurationManagement.ConfigurationManagerSectionHandler,Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
  <section name="OtherConfigFile" type="testa.CustomSectionHandler,testa, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
 </configSections>
 <applicationConfigurationManagement defaultSection="OtherConfigFile">
  <configSection name="OtherConfigFile">
   <configProvider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"
    type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage" signed="false"
    refreshOnChange="true" encrypted="false" path="../../otherConfigFile.config" />
   <protectionProvider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"
         type="Microsoft.ApplicationBlocks.ConfigurationManagement.DataProtection.BCLDataProtection"
         hashKey="MyXuEd6f+go=" symmetricKey="VToaqZjp8C27V90oSmT/CF+afvRGClc9" initializationVector="ou95G2/WziI="/>   
   
  </configSection>
 </applicationConfigurationManagement>
</configuration>

<configuration><section>项是.net框架定义的元素,详见MSDN (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpgenref/html/gngrfsectionelement.htm)
applicationConfigurationManagement 是CMAB的配置项configSection 指定程序读取的配置项的名称
configProvider 提供要使用到CMAB的组件的名称,使用什么方式来读写配置文件,自定义配置文件
   路径和是否加密等信息。
protectionProvider 提供加密的组件名称
5. 添加完配置文件后在程序中再添加一个新的XML配置文件,文件名为OtherConfigFile,然后在此文件中添加如下代码。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <OtherConfigFile>
    <CustomConfigurationData xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <name>tiger</name>
      <age>30</age>
    </CustomConfigurationData>
  </OtherConfigFile>
</configuration>

此段XML配置文件是自定义的配置文件其中configuration、OtherConfigFile、CustomConfigurationData是固定的XML标识。
包括在CustomConfigurationData里面的nameage两个元素为自定义元素。

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的类,此类是用来控制读取和写入配置文件的,它实现了IconfigurationSectionHandlerIconfigurationSectionHandlerWriter两个接口。里面提供的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上配别填写“姓名”和“年龄”
 两个文本框分别命名为txtNametxtAge
 把button1name改为btnReadtext属性改为“读取”,button2name属性改为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的话,就可以实现加密了

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="applicationConfigurationManagement" type="Microsoft.ApplicationBlocks.ConfigurationManagement.ConfigurationManagerSectionHandler,Microsoft.ApplicationBlocks.ConfigurationManagement, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
  <section name="OtherConfigFile" type="testa.CustomSectionHandler,testa, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
 </configSections>
 <applicationConfigurationManagement defaultSection="UnencryptedXml">
  <configSection name="OtherConfigFile">
   <configCache enabled="true" refresh="1 * * * *" />
   <configProvider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"
    type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage" signed="false"
    refreshOnChange="true" encrypted="true" path="../../otherConfigFile.config" />
   <protectionProvider assembly="Microsoft.ApplicationBlocks.ConfigurationManagement,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"
         type="Microsoft.ApplicationBlocks.ConfigurationManagement.DataProtection.BCLDataProtection"
         hashKey="MyXuEd6f+go=" symmetricKey="VToaqZjp8C27V90oSmT/CF+afvRGClc9" initializationVector="ou95G2/WziI="/>
    
   
  </configSection>
 </applicationConfigurationManagement>
</configuration>
5. 添加完配置文件后在程序中再添加一个新的XML配置文件,文件名为OtherConfigFile,然后在此文件中添加如下代码。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <OtherConfigFile>
    <CustomConfigurationData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <name>tiger</name>
      <age>30</age>
    </CustomConfigurationData>
  </OtherConfigFile>
</configuration>

 

分类: C和C# 标签: 2,493 次阅读
原文链接:http://www.wenhq.com/article/view_55.html
欢迎转载,请注明出处:亲亲宝宝
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.