Configuration Management Application Block (CMAB) Method

xiaoxiao2021-03-06  82

CMAB usage

CMAB use method 1. Create a new WinForm project Testa. 2. References Microsoft.ApplicationBlocks.configurationManagement.dll and Microsoft.ApplicationBlocks.configurationManagement.Interfaces.dll two class libraries. 3. Import Namespaces in the program Using Microsoft.ApplicationBlocks.configurationManagement; 4. Right-click on the project in the Solution Manager, Add-"Add a new item -" Application Profile ", name the newly added configuration file app.config Click Open Add Profile.

Add the following code in the configuration file after the addition is completed:

Item is the element defined by the .NET framework, see MSDN (MS-Help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/cpgenref/html/gngrfselement.htm) ApplicationConfigurationManagement is the CMAB configuration item ConfigSECTION specifies the name of the configuration item read for the program ConfigProvider provides the name of the component to use to use, what way is used to read the configuration file, customize the configuration file path, and whether to encrypt. ProtectionProvider provides encrypted component names 5. Add a new XML configuration file in the program after adding the configuration file, the file name is OtherConfigfile, then add the following code to this file. Tiger 30 This XML configuration file is Customized profile where configuration, OtherConfigfile, CustomConfigurationData are fixed XML IDs. Includes Name inside CustomFigurationData, AGE two elements are custom elements. 6. Create a class called CustomFigurationData.cs, ​​such a serialized class define the following two properties in the class, these two attributes are named and age public string name {get {return_name;} set {_name = value ;}} String _name; public string age; {get {return _age;}} String_age; 7. Categories called CustomSECTIONHANDLER.CS, which are used to control read and write profiles, which implements two interfaces for IconfigurationSectionHandler and IconfigurationSectionHandlerWriter. The CREATE method provided inside is to read the configuration file data, serialize is used to write configuration file data.

(Namespace program loaded into the two classes below) [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 ("This configuration item cannot be serialized!", E);}}} 8. Add two text boxes on the form, two lable and two buttons fill in the name of the two lable text "Two" age " This box is named TXTNAME and TXTAGE. Change the name of Button1 to btnread, and the text property is changed to "Read". Button2's Name property is changed to btnWrite, and the text property is changed to "write". 9. btnRead click event which add the following code CustomConfigurationData cclass = (CustomConfigurationData) ConfigurationManager.Read ( "OtherConfigFile"); txtName.Text = cclass.name; txtAge.Text = cclass.age; 10. click of btnWrite add the following code in the event CustomConfigurationData cf = new CustomConfigurationData (); cf.name = txtName.Text.Trim (); cf.age = txtAge.Text.Trim (); ConfigurationManager.Write ( "OtherConfigFile", cf); the above The procedure can be read and write the configuration file after running the program.

If you use the encrypted set to true, you can achieve encrypted

< 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 "pat =" ../../ otherconfigfile.config "/> <

转载请注明原文地址:https://www.9cbs.com/read-94831.html

New Post(0)