public static void WriteConfig(MyFormState state, string queryText, string paramsText, string cmbStyleText, string serverText, string captionText, string footerText, string authText, string userName, string passText, string databaseText, string rotateText, string rcoText, string customStyleText, string listConnection) { var ws = new XmlWriterSettings { NewLineHandling = NewLineHandling.Entitize }; state = new MyFormState { ListBoxText = listConnection, QueryText = queryText, ParamText = paramsText, StyleText = cmbStyleText, ServerText = serverText, CaptionText = captionText, FooterText = footerText, AuthText = authText, UserNameText = userName, PasswordText = passText, DataBaseText = databaseText, RotateText = rotateText, RcoText = rcoText, CustomStyleText = customStyleText }; var ser = new XmlSerializer(typeof(MyFormState)); using (var wr = XmlWriter.Create(ConfigurationManager.AppSettings["fileToSaveInput"], ws)) { ser.Serialize(wr, state); } //ser.Serialize(sw, state); }
public static MyFormState LoadConfig() { var ser = new XmlSerializer(typeof(MyFormState)); using (var fs = File.OpenRead("config.xml")) { MyFormState state = null; try { state = (MyFormState)ser.Deserialize(fs); } catch (Exception ex) { state = null; } return(state); } }
private void Form1_Load(object sender, System.EventArgs e) { //Determine is load appdomain button is visible btnLoadAppDomain.Visible = Convert.ToBoolean(ConfigurationManager.AppSettings["showLoadAppDomainButton"]); btnUnloadAppDomain.Visible = Convert.ToBoolean(ConfigurationManager.AppSettings["showUnLoadAppDomainButton"]); _assemblyName = ConfigurationManager.AppSettings["assemblyName"]; //Create output directory if not exists if (Directory.Exists(ConfigurationManager.AppSettings["outputPath"]) == false) { try { Directory.CreateDirectory(ConfigurationManager.AppSettings["outputPath"]); } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } else { MessageBox.Show( $@"Unable to create default directory.Please change path in app.config {ex.Message}"); } } } var codeToCompile = ConfigurationManager.AppSettings["codeToCompile"]; _compObj = _se.Compile(codeToCompile.Trim(), true, "SqlServerCentral", ref _errorMessage); //Fill styles FillStyles(); //Restore user inputs if (Convert.ToBoolean(ConfigurationManager.AppSettings["saveInput"]) && File.Exists(ConfigurationManager.AppSettings["fileToSaveInput"])) { _state = Helper.LoadConfig(); if (_state != null) { try { txtQuery.Text = _state.QueryText; txtParams.Text = _state.ParamText; txtCaption.Text = _state.CaptionText; txtFooter.Text = _state.FooterText; if (_state.StyleText != null) { cmbStyle.SelectedIndex = cmbStyle.FindStringExact(_state.StyleText); } txtServer.Text = _state.ServerText; if (cmbAuth.SelectedIndex != -1) { cmbAuth.SelectedIndex = cmbAuth.FindStringExact(_state.AuthText); } txtUserName.Text = _state.UserNameText; try { txtPassword.Text = _wrapper.DecryptData(_state.PasswordText); } catch (Exception ex) { if (Debugger.IsAttached) { MessageBox.Show($@"Exception :{ex.Message}"); Debugger.Break(); } } if (txtUserName.Text.Trim().Equals(string.Empty) && txtPassword.Text.Trim().Equals(string.Empty)) { cmbAuth.SelectedIndex = 0; } //bind database information BindDataBases(); cmbDatabase.SelectedIndex = cmbDatabase.FindStringExact(_state.DataBaseText); cmbRotate.SelectedIndex = cmbRotate.FindStringExact(_state.RotateText); if (cmbRotate.SelectedIndex == -1) { cmbRotate.SelectedIndex = 0; } txtRCO.Value = Convert.ToInt32(_state.RcoText); txtCustomStyle.Text = _state.CustomStyleText; if (_state.ListBoxText != null) { MakeListConnection(_state.ListBoxText.Split(new[] { Environment.NewLine }, StringSplitOptions.None)); } } catch (Exception ex) { if (Debugger.IsAttached) { Debugger.Break(); } else { MessageBox.Show(ex.Message); } } } } else { //If we do not want saving inputs, defaults are blue style and windows auth cmbStyle.SelectedIndex = 1; cmbAuth.SelectedIndex = 0; cmbRotate.SelectedIndex = 1; } //put focus on this control if (listBoxConnection.Items.Count > 0) { listBoxConnection.SelectedIndex = 0; BindAssemblyList(_assemblyName); } txtServer.Select(); }