public static void SaveConnectionsAs() { ConnectionsSaver connectionsSave = new ConnectionsSaver(); try { if (SQLConnProvider != null) { SQLConnProvider.Disable(); } using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.CheckPathExists = true; saveFileDialog.InitialDirectory = ConnectionsFileInfo.DefaultConnectionsPath; saveFileDialog.FileName = ConnectionsFileInfo.DefaultConnectionsFile; saveFileDialog.OverwritePrompt = true; List <string> fileTypes = new List <string>(); fileTypes.AddRange(new[] { Language.strFiltermRemoteXML, "*.xml" }); fileTypes.AddRange(new[] { Language.strFilterAll, "*.*" }); saveFileDialog.Filter = string.Join("|", fileTypes.ToArray()); if (saveFileDialog.ShowDialog(frmMain.Default) != DialogResult.OK) { return; } connectionsSave.SaveFormat = ConnectionsSaver.Format.mRXML; connectionsSave.ConnectionFileName = saveFileDialog.FileName; connectionsSave.Export = false; connectionsSave.SaveSecurity = new Security.Save(); connectionsSave.ConnectionList = ConnectionList; connectionsSave.ContainerList = ContainerList; connectionsSave.RootTreeNode = Windows.treeForm.tvConnections.Nodes[0]; connectionsSave.SaveConnections(); if (saveFileDialog.FileName == GetDefaultStartupConnectionFileName()) { Settings.Default.LoadConsFromCustomLocation = false; } else { Settings.Default.LoadConsFromCustomLocation = true; Settings.Default.CustomConsPath = saveFileDialog.FileName; } } } catch (Exception ex) { MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotSaveAs, connectionsSave.ConnectionFileName), ex); } finally { SQLConnProvider?.Enable(); } }
public static void SaveConnections(bool Update = false) { if (!IsConnectionsFileLoaded) { return; } try { if (Update && Settings.Default.UseSQLServer == false) { return; } if (SQLConnProvider != null) { SQLConnProvider.Disable(); } ConnectionsSaver conS = new ConnectionsSaver(); if (!Settings.Default.UseSQLServer) { conS.ConnectionFileName = GetStartupConnectionFileName(); } conS.ConnectionList = ConnectionList; conS.ContainerList = ContainerList; conS.Export = false; conS.SaveSecurity = new Security.Save(); conS.RootTreeNode = Windows.treeForm.tvConnections.Nodes[0]; if (Settings.Default.UseSQLServer) { conS.SaveFormat = ConnectionsSaver.Format.SQL; conS.SQLHost = Convert.ToString(Settings.Default.SQLHost); conS.SQLDatabaseName = Convert.ToString(Settings.Default.SQLDatabaseName); conS.SQLUsername = Convert.ToString(Settings.Default.SQLUser); var cryptographyProvider = new LegacyRijndaelCryptographyProvider(); conS.SQLPassword = cryptographyProvider.Decrypt(Convert.ToString(Settings.Default.SQLPass), GeneralAppInfo.EncryptionKey); } conS.SaveConnections(); if (Settings.Default.UseSQLServer) { LastSqlUpdate = DateTime.Now; } } catch (Exception ex) { MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strConnectionsFileCouldNotBeSaved + Environment.NewLine + ex.Message); } finally { SQLConnProvider?.Enable(); } }
public static void LoadConnections(bool withDialog = false, bool update = false) { var connectionsLoader = new ConnectionsLoader(); try { // disable sql update checking while we are loading updates SQLConnProvider?.Disable(); if (ConnectionList != null && ContainerList != null) { PreviousConnectionList = ConnectionList.Copy(); PreviousContainerList = ContainerList.Copy(); } ConnectionList = new ConnectionList(); ContainerList = new ContainerList(); if (!Settings.Default.UseSQLServer) { if (withDialog) { var loadDialog = Tools.Controls.ConnectionsLoadDialog(); if (loadDialog.ShowDialog() != DialogResult.OK) { return; } connectionsLoader.ConnectionFileName = loadDialog.FileName; } else { connectionsLoader.ConnectionFileName = GetStartupConnectionFileName(); } CreateBackupFile(Convert.ToString(connectionsLoader.ConnectionFileName)); } connectionsLoader.ConnectionList = ConnectionList; connectionsLoader.ContainerList = ContainerList; if (PreviousConnectionList != null && PreviousContainerList != null) { connectionsLoader.PreviousConnectionList = PreviousConnectionList; connectionsLoader.PreviousContainerList = PreviousContainerList; } if (update) { connectionsLoader.PreviousSelected = LastSelected; } ConnectionTree.ResetTree(); connectionsLoader.RootTreeNode = Windows.treeForm.tvConnections.Nodes[0]; connectionsLoader.UseDatabase = Settings.Default.UseSQLServer; connectionsLoader.DatabaseHost = Settings.Default.SQLHost; connectionsLoader.DatabaseName = Settings.Default.SQLDatabaseName; connectionsLoader.DatabaseUsername = Settings.Default.SQLUser; var cryptographyProvider = new LegacyRijndaelCryptographyProvider(); connectionsLoader.DatabasePassword = cryptographyProvider.Decrypt(Convert.ToString(Settings.Default.SQLPass), GeneralAppInfo.EncryptionKey); connectionsLoader.DatabaseUpdate = update; connectionsLoader.LoadConnections(false); if (Settings.Default.UseSQLServer) { LastSqlUpdate = DateTime.Now; } else { if (connectionsLoader.ConnectionFileName == GetDefaultStartupConnectionFileName()) { Settings.Default.LoadConsFromCustomLocation = false; } else { Settings.Default.LoadConsFromCustomLocation = true; Settings.Default.CustomConsPath = connectionsLoader.ConnectionFileName; } } // re-enable sql update checking after updates are loaded SQLConnProvider?.Enable(); } catch (Exception ex) { if (Settings.Default.UseSQLServer) { MessageCollector.AddExceptionMessage(Language.strLoadFromSqlFailed, ex); var commandButtons = string.Join("|", Language.strCommandTryAgain, Language.strCommandOpenConnectionFile, string.Format(Language.strCommandExitProgram, Application.ProductName)); CTaskDialog.ShowCommandBox(Application.ProductName, Language.strLoadFromSqlFailed, Language.strLoadFromSqlFailedContent, MiscTools.GetExceptionMessageRecursive(ex), "", "", commandButtons, false, ESysIcons.Error, ESysIcons.Error); switch (CTaskDialog.CommandButtonResult) { case 0: LoadConnections(withDialog, update); return; case 1: Settings.Default.UseSQLServer = false; LoadConnections(true, update); return; default: Application.Exit(); return; } } if (ex is FileNotFoundException && !withDialog) { MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoadedNew, connectionsLoader.ConnectionFileName), ex, MessageClass.InformationMsg); NewConnections(Convert.ToString(connectionsLoader.ConnectionFileName)); return; } MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoaded, connectionsLoader.ConnectionFileName), ex); if (connectionsLoader.ConnectionFileName != GetStartupConnectionFileName()) { LoadConnections(withDialog, update); } else { MessageBox.Show(frmMain.Default, string.Format(Language.strErrorStartupConnectionFileLoad, Environment.NewLine, Application.ProductName, GetStartupConnectionFileName(), MiscTools.GetExceptionMessageRecursive(ex)), "Could not load startup file.", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } }