繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 评论及其它 >> Configuration Management Application Block (cmab)使用方法

Configuration Management Application Block (cmab)使用方法

2005-01-09 20:55:27  作者:qqws  来源:互联网  浏览次数:1  文字大小:【】【】【
简介:  Cmab使用方法 1. 新建一个WINform项目 testa。2. 引用Microsoft.ApplicationBlocks.ConfigurationManagement.dll和Microsoft.ApplicationBlocks.ConfigurationManagement.Interfaces.dll两...

Cmab使用方法

1. 新建一个WINform项目 testa。

2. 引用Microsoft.ApplicationBlocks.ConfigurationManagement.dll和Microsoft.ApplicationBlocks.ConfigurationManagement.Interfaces.dll两个类库。

3. 在程序里面导入命名空间 using Microsoft.ApplicationBlocks.ConfigurationManagement;

4. 在解决方案管理器里的项目上单击鼠标右键,添加-》添加新项-》应用程序配置文件,把新添加的配置文件命名为App.Config 单击打开添加配置文件。

当添加完成后就在配置文件中添加如下代码:

type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage" signed="false"

refreshOnChange="true" encrypted="false" path="../../otherConfigFile.config" />

type="Microsoft.ApplicationBlocks.ConfigurationManagement.DataProtection.BCLDataProtection"

hashKey="MyXuEd6f+go=" symmetricKey="VToaqZjp8C27V90oSmT/CF+afvRGClc9" initializationVector="ou95G2/WziI="/>

5. 添加完配置文件后在程序中再添加一个新的XML配置文件,文件名为OtherConfigFile,然后在此文件中添加如下代码。

tiger

30

此段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的话,就可以实现加密了

type="Microsoft.ApplicationBlocks.ConfigurationManagement.Storage.XmlFileStorage" signed="false"

refreshOnChange="true" encrypted="true" path="../../otherConfigFile.config" />

type="Microsoft.ApplicationBlocks.ConfigurationManagement.DataProtection.BCLDataProtection"

hashKey="MyXuEd6f+go=" symmetricKey="VToaqZjp8C27V90oSmT/CF+afvRGClc9" initializationVector="ou95G2/WziI="/>

5. 添加完配置文件后在程序中再添加一个新的XML配置文件,文件名为OtherConfigFile,然后在此文件中添加如下代码。

tiger

30

责任编辑:admin
相关文章