コード例 #1
0
        void SaveConfiguration(string configurationFile, BindableProperties properties)
        {
            ServiceController controller = new ServiceController();
            bool restartService          = false;

            StringBuilder returnMessage = new StringBuilder();

            try
            {
                if (CheckBoxAutoRestart.Checked && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultServiceName"]))       //then stop service, modify config file and then start service.
                {
                    controller.ServiceName = ConfigurationManager.AppSettings["DefaultServiceName"];
                    if (controller.CanStop && controller.Status != ServiceControllerStatus.Stopped && controller.Status != ServiceControllerStatus.StopPending)
                    {
                        controller.Stop();
                        controller.WaitForStatus(ServiceControllerStatus.Stopped);
                    }
                    restartService = true;
                    returnMessage.AppendLine("Successfully stopped " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service.");
                    returnMessage.AppendLine();
                }
            }
            catch (Exception ex)
            {
                returnMessage.AppendLine("ERROR: Failed to stop " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service." + Environment.NewLine + ex.Message + Environment.NewLine + " Failed to save configuration changes.");
                returnMessage.AppendLine();
                return;
            }

            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 = properties.GetProperties();

                foreach (string section in m_categorizedSections)
                {
                    RepopulateXmlSection(section, xmlDoc, props);
                }

                xmlDoc.Save(configurationFile);

                returnMessage.AppendLine("Successfully saved configuration changes to " + Environment.NewLine + this.ConfigurationFileName);
                returnMessage.AppendLine();

                this.IsDirty = false;
            }
            catch (Exception ex)
            {
                returnMessage.AppendLine("ERROR: Failed to save configuration changes: " + ex.Message);
                returnMessage.AppendLine();
            }

            try
            {
                if (restartService && controller.Status == ServiceControllerStatus.Stopped)
                {
                    controller.Start();
                    returnMessage.AppendLine("Successfully started " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service.");
                    returnMessage.AppendLine();
                }
            }
            catch (Exception ex)
            {
                returnMessage.AppendLine("ERROR: Failed to start " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service." + Environment.NewLine + ex.Message);
                returnMessage.AppendLine();
            }

            MessageBox.Show(returnMessage.ToString());
        }
コード例 #2
0
ファイル: Main.cs プロジェクト: rmc00/gsf
        void SaveConfiguration(string configurationFile, BindableProperties properties)
        {
            ServiceController controller = new ServiceController();
            bool restartService = false;

            StringBuilder returnMessage = new StringBuilder();

            try
            {
                if (CheckBoxAutoRestart.Checked && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultServiceName"]))	//then stop service, modify config file and then start service.
                {
                    controller.ServiceName = ConfigurationManager.AppSettings["DefaultServiceName"];
                    if (controller.CanStop && controller.Status != ServiceControllerStatus.Stopped && controller.Status != ServiceControllerStatus.StopPending)
                    {
                        controller.Stop();
                        controller.WaitForStatus(ServiceControllerStatus.Stopped);
                    }
                    restartService = true;
                    returnMessage.AppendLine("Successfully stopped " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service.");
                    returnMessage.AppendLine();
                }
            }
            catch (Exception ex)
            {
                returnMessage.AppendLine("ERROR: Failed to stop " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service." + Environment.NewLine + ex.Message + Environment.NewLine + " Failed to save configuration changes.");
                returnMessage.AppendLine();
                return;
            }

            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 = properties.GetProperties();

                foreach (string section in m_categorizedSections)
                {
                    RepopulateXmlSection(section, xmlDoc, props);
                }

                xmlDoc.Save(configurationFile);

                returnMessage.AppendLine("Successfully saved configuration changes to " + Environment.NewLine + this.ConfigurationFileName);
                returnMessage.AppendLine();

                this.IsDirty = false;
            }
            catch (Exception ex)
            {
                returnMessage.AppendLine("ERROR: Failed to save configuration changes: " + ex.Message);
                returnMessage.AppendLine();
            }

            try
            {
                if (restartService && controller.Status == ServiceControllerStatus.Stopped)
                {
                    controller.Start();
                    returnMessage.AppendLine("Successfully started " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service.");
                    returnMessage.AppendLine();
                }
            }
            catch (Exception ex)
            {
                returnMessage.AppendLine("ERROR: Failed to start " + ConfigurationManager.AppSettings["DefaultServiceName"] + " service." + Environment.NewLine + ex.Message);
                returnMessage.AppendLine();
            }

            MessageBox.Show(returnMessage.ToString());
        }
コード例 #3
0
        BindableProperties LoadConfigurationSettings(string configurationFile)
        {
            BindableProperties properties = new BindableProperties();

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configurationFile);
                XmlNode     configuration = xmlDoc.SelectSingleNode("configuration/categorizedSettings");
                XmlNodeList sectionList   = configuration.ChildNodes;

                for (int y = 0; y < sectionList.Count; y++)
                {
                    XmlNodeList settingsList = xmlDoc.SelectNodes("configuration/categorizedSettings/" + sectionList[y].Name + "/add");

                    if (!m_categorizedSections.Contains(sectionList[y].Name))
                    {
                        m_categorizedSections.Add(sectionList[y].Name);
                    }

                    if (settingsList.Count != 0 && settingsList != null)
                    {
                        for (int i = 0; i < settingsList.Count; i++)
                        {
                            XmlAttribute atrribKey         = settingsList[i].Attributes["name"];
                            XmlAttribute attribValue       = settingsList[i].Attributes["value"];
                            XmlAttribute attribDescription = settingsList[i].Attributes["description"];
                            XmlAttribute attribEncrypted   = settingsList[i].Attributes["encrypted"];
                            if (atrribKey != null && attribValue != null)
                            {
                                //If there's no description for the key - assign the name to the description.
                                if (attribDescription == null)
                                {
                                    attribDescription = atrribKey;
                                }

                                Type propType;
                                //string selectedValue = string.Empty;

                                if (attribValue.Value.ToLower() == "true" || attribValue.Value.ToLower() == "false")
                                {
                                    propType = typeof(Boolean);
                                }
                                else
                                {
                                    propType = typeof(String);
                                }

                                //If value is encrypted, then decrypt before loading it into property grid.
                                string attribValueString;
                                if (attribEncrypted != null && attribEncrypted.Value.ToLower() == "true" && !string.IsNullOrEmpty(attribValue.Value))
                                {
                                    attribValueString = attribValue.Value.Decrypt(DefaultCryptoKey, CryptoStrength);
                                }
                                else
                                {
                                    attribValueString = attribValue.Value;
                                }

                                //Now add the property
                                properties.AddProperty(atrribKey.Value, attribValueString,
                                                       attribDescription.Value, sectionList[y].Name, propType, false, false);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return(properties);
        }
コード例 #4
0
ファイル: Main.cs プロジェクト: rmc00/gsf
        BindableProperties LoadConfigurationSettings(string configurationFile)
        {
            BindableProperties properties = new BindableProperties();
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configurationFile);
                XmlNode configuration = xmlDoc.SelectSingleNode("configuration/categorizedSettings");
                XmlNodeList sectionList = configuration.ChildNodes;

                for (int y = 0; y < sectionList.Count; y++)
                {
                    XmlNodeList settingsList = xmlDoc.SelectNodes("configuration/categorizedSettings/" + sectionList[y].Name + "/add");

                    if (!m_categorizedSections.Contains(sectionList[y].Name))
                        m_categorizedSections.Add(sectionList[y].Name);

                    if (settingsList.Count != 0 && settingsList != null)
                    {
                        for (int i = 0; i < settingsList.Count; i++)
                        {
                            XmlAttribute atrribKey = settingsList[i].Attributes["name"];
                            XmlAttribute attribValue = settingsList[i].Attributes["value"];
                            XmlAttribute attribDescription = settingsList[i].Attributes["description"];
                            XmlAttribute attribEncrypted = settingsList[i].Attributes["encrypted"];
                            if (atrribKey != null && attribValue != null)
                            {
                                //If there's no description for the key - assign the name to the description.								
                                if (attribDescription == null)
                                    attribDescription = atrribKey;

                                Type propType;
                                //string selectedValue = string.Empty;

                                if (attribValue.Value.ToLower() == "true" || attribValue.Value.ToLower() == "false")
                                    propType = typeof(Boolean);
                                else
                                    propType = typeof(String);

                                //If value is encrypted, then decrypt before loading it into property grid.
                                string attribValueString;
                                if (attribEncrypted != null && attribEncrypted.Value.ToLower() == "true" && !string.IsNullOrEmpty(attribValue.Value))
                                    attribValueString = attribValue.Value.Decrypt(DefaultCryptoKey, CryptoStrength);
                                else
                                    attribValueString = attribValue.Value;

                                //Now add the property													
                                properties.AddProperty(atrribKey.Value, attribValueString,
                                        attribDescription.Value, sectionList[y].Name, propType, false, false);

                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return properties;
        }