Exemple #1
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                var appSettings = ConfigurationManager.AppSettings;
                try
                {
                    var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var settings   = configFile.AppSettings.Settings;

                    if (settings[type.Name] == null)
                    {
                        settings.Add(type.Name, LastTouch.ToString());
                    }
                    else
                    {
                        settings[type.Name].Value = LastTouch.ToString();
                    }

                    if (settings["RefreshRate"] == null)
                    {
                        settings.Add("RefreshRate", (RefreshRate / 1000).ToString());
                    }
                    else
                    {
                        settings["RefreshRate"].Value = (RefreshRate / 1000).ToString();
                    }

                    if (settings["UseAscii"] == null)
                    {
                        settings.Add("UseAscii", UseAscii.ToString());
                    }
                    else
                    {
                        settings["UseAscii"].Value = UseAscii.ToString();
                    }

                    configFile.Save(ConfigurationSaveMode.Modified);
                    //ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
                }
                catch (Exception ex)
                {
                    EventLogger.Error($"Failed to save configuration: {ex.Message}");
                    throw;
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (configuration.AppSettings.Settings[ConfigurationParameters.LogFontSize] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.LogFontSize, LogFontSize.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.LogFontSize].Value = LogFontSize.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.TreeViewFontSize] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.TreeViewFontSize, TreeViewFontSize.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.TreeViewFontSize].Value = TreeViewFontSize.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.ShowMessageCountParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.ShowMessageCountParameter, ShowMessageCount.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.ShowMessageCountParameter].Value = ShowMessageCount.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SaveMessageToFileParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SaveMessageToFileParameter, SaveMessageToFile.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SaveMessageToFileParameter].Value = SaveMessageToFile.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.UseAsciiParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.UseAsciiParameter, UseAscii.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.UseAsciiParameter].Value = UseAscii.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SavePropertiesToFileParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SavePropertiesToFileParameter, SavePropertiesToFile.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SavePropertiesToFileParameter].Value = SavePropertiesToFile.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SaveCheckpointsToFileParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SaveCheckpointsToFileParameter, SaveCheckpointsToFile.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SaveCheckpointsToFileParameter].Value = SaveCheckpointsToFile.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.RetryCountParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.RetryCountParameter, RetryCount.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.RetryCountParameter].Value = RetryCount.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.RetryTimeoutParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.RetryTimeoutParameter, RetryTimeout.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.RetryTimeoutParameter].Value = RetryTimeout.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.TopParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.TopParameter, TopCount.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.TopParameter].Value = TopCount.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.ReceiveTimeoutParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.ReceiveTimeoutParameter, ReceiveTimeout.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.ReceiveTimeoutParameter].Value = ReceiveTimeout.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.ServerTimeoutParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.ServerTimeoutParameter, ServerTimeout.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.ServerTimeoutParameter].Value = ServerTimeout.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SenderThinkTimeParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SenderThinkTimeParameter, SenderThinkTime.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SenderThinkTimeParameter].Value = SenderThinkTime.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SenderThinkTimeParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SenderThinkTimeParameter, SenderThinkTime.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SenderThinkTimeParameter].Value = SenderThinkTime.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.ReceiverThinkTimeParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.ReceiverThinkTimeParameter, ReceiverThinkTime.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.ReceiverThinkTimeParameter].Value = ReceiverThinkTime.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.MonitorRefreshIntervalParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.MonitorRefreshIntervalParameter, MonitorRefreshInterval.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.MonitorRefreshIntervalParameter].Value = MonitorRefreshInterval.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.PrefetchCountParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.PrefetchCountParameter, PrefetchCount.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.PrefetchCountParameter].Value = PrefetchCount.ToString(CultureInfo.InvariantCulture);
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SubscriptionIdParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SubscriptionIdParameter, SubscriptionId);
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SubscriptionIdParameter].Value = SubscriptionId;
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.CertificateThumbprintParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.CertificateThumbprintParameter, CertificateThumbprint);
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.CertificateThumbprintParameter].Value = CertificateThumbprint;
            }


            if (configuration.AppSettings.Settings[ConfigurationParameters.LabelParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.LabelParameter, Label);
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.LabelParameter].Value = Label;
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.FileParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.FileParameter, MessageFile);
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.FileParameter].Value = MessageFile;
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.MessageParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.MessageParameter, MessageText);
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.MessageParameter].Value = MessageText;
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.ConnectivityMode] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.ConnectivityMode, ServiceBusHelper.ConnectivityMode.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.ConnectivityMode].Value = ServiceBusHelper.ConnectivityMode.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.Encoding] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.Encoding, ServiceBusHelper.EncodingType.ToString());
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.Encoding].Value = ServiceBusHelper.EncodingType.ToString();
            }
            if (configuration.AppSettings.Settings[ConfigurationParameters.SelectedEntitiesParameter] == null)
            {
                configuration.AppSettings.Settings.Add(ConfigurationParameters.SelectedEntitiesParameter, cboSelectedEntities.Text);
            }
            else
            {
                configuration.AppSettings.Settings[ConfigurationParameters.SelectedEntitiesParameter].Value = cboSelectedEntities.Text;
            }
            configuration.Save(ConfigurationSaveMode.Minimal);
        }