Beispiel #1
0
 /// <summary>
 /// We're only supporting three sections here at the moment.
 /// The default appSettings, plus our standard ApplicationConfiguration
 /// and CommonConfiguration section handlers. These handlers have extended support for
 /// the Description attribute in addition to the Key, Value pair attributes
 /// in the Xml configuration file.
 /// </summary>
 public void SaveConfiguration(string configurationFile, CustomClass customClass)
 {
     try
     {
         //Reload the configuration file
         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.Load(configurationFile);
         //Save a backup version
         xmlDoc.Save(configurationFile + "_bak");
         //Populate our property collection.
         PropertyDescriptorCollection props = customClass.GetProperties();
         //Repolulate the three supported sections
         RepopulateXmlSection("ApplicationConfiguration", xmlDoc, props);
         RepopulateXmlSection("CommonConfiguration", xmlDoc, props);
         RepopulateXmlSection("appSettings", xmlDoc, props);
         xmlDoc.Save(configurationFile);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 /// <summary>
 /// This method will look at the Maximum value of either a property name or it's
 /// value (which ever is greater) and adjust the width of the form accordingly.
 /// The Property Grid is set to Anchor Left, Top and Right so it will expand along
 /// with the form.
 /// </summary>
 /// <param name="customClass"></param>
 private void LayoutForm(CustomClass customClass)
 {
     //MaxLength is the number of characters. At our current
     //font size we need to allow for an acceptable multiplier in pixels.
     if (customClass.MaxLength > 95)
     {
         this.Width = 665;
     }
     else
     {
         this.Width = customClass.MaxLength  * 7;
     }
 }
 /// <summary>
 /// We're only supporting three sections here at the moment.
 /// The default appSettings, plus our standard ApplicationConfiguration
 /// and CommonConfiguration section handlers. These handlers have extended support for
 /// the Description attribute in addition to the Key, Value pair attributes 
 /// in the Xml configuration file.
 /// </summary>
 public void SaveConfiguration(string configurationFile, CustomClass customClass)
 {
     try
     {
         //Reload the configuration file
         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.Load(configurationFile);
         //Save a backup version
         xmlDoc.Save(configurationFile + "_bak");
         //Populate our property collection.
         PropertyDescriptorCollection props = customClass.GetProperties();
         //Repolulate the three supported sections
         RepopulateXmlSection("ApplicationConfiguration", xmlDoc, props);
         RepopulateXmlSection("CommonConfiguration", xmlDoc, props);
         RepopulateXmlSection("appSettings", xmlDoc, props);
         xmlDoc.Save(configurationFile);
     }
     catch(Exception ex)
     {
         throw ex;
     }
 }
        /// <summary>
        /// Load the Xml Configuration document and populate our CustomClass with a dynamic property
        /// for each of the supported configuration sections. We're only supporting three sections here.
        /// The default appSettings, plus our standard ApplicationConfiguration
        /// and CommonConfiguration section handlers. These handlers are derived from IConfigurationSectionHandler.
        /// They have extended support for the Description attribute in addition to the Key, Value 
        /// pair attributes in the Xml configuration file.
        /// This could easily be extended to include support for any section under the configuration
        /// section that has the <add key="value" value="value"/> structure (assuming you haven't written a
        /// completely new Xml structure for your custom configuration section).
        /// </summary>
        public CustomClass LoadConfiguration(string configurationFile)
        {
            CustomClass customClass = new CustomClass();
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configurationFile);
                XmlNode configuration = xmlDoc.SelectSingleNode("configuration");

                //Build the node list
                XmlNodeList sectionList = configuration.ChildNodes;
                for(int y = 0; y <  sectionList.Count; y++)
                {
                    //XmlNodeList settingsList = xmlDoc.SelectNodes("configuration/" + sectionList[y].Name + "/add");
                    XmlNodeList settingsList = xmlDoc.SelectNodes("configuration/userSettings/Software_Inspector.Properties.Settings");
                    if (settingsList.Count != 0 && settingsList != null)
                    {
                        //Add a property to customClass for each node found.
                        for(int i = 0; i <  settingsList.Count; i++)
                        {
                            XmlAttribute atrribKey = settingsList[i].Attributes["key"];
                            XmlAttribute attribValue = settingsList[i].Attributes["value"];
                            XmlAttribute attribDescription = settingsList[i].Attributes["description"];
                            if(atrribKey != null && attribValue != null)
                            {
                                //If there's no description for the key - assign the name to the description.
                                //The description attribute is displayed below the name in the property grid.
                                if (attribDescription == null)
                                {
                                    attribDescription = atrribKey;
                                }
                                //We'll at least test to see if it's a boolean property and set the type here
                                //to force the property grid to display a dropdown list of True or False.
                                Type propType;
                                if (attribValue.Value.ToLower() == "true" || attribValue.Value.ToLower() == "false")
                                {
                                    propType = typeof(System.Boolean);
                                }
                                else
                                {
                                    propType = typeof(System.String);
                                }
                                //Now add the property
                                customClass.AddProperty(atrribKey.Value.ToString(), attribValue.Value.ToString(),
                                    attribDescription.Value.ToString(), sectionList[y].Name, propType, false, false);
                            }
                        }
                    }
                }
                xmlDoc = null;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return customClass;
        }