Exemplo n.º 1
0
        private void SaveConfig()
        {
            Program.ConfigMgt.Config.ConfigDBConnection = this.textBoxDBConnectionConfig.Text;
            Program.ConfigMgt.Config.DataDBConnection   = this.textBoxDBConnectionData.Text;
            Program.ConfigMgt.Config.OSqlFileName       = this.textBoxOSQLPath.Text;
            Program.ConfigMgt.Config.OSqlParameter      = this.textBoxOSQLArg.Text;

            if (this.comboBoxView.SelectedIndex == 0)
            {
                Program.ConfigMgt.Config.ShowDeviceViewWhenStartup = true;
            }
            else
            {
                Program.ConfigMgt.Config.ShowDeviceViewWhenStartup = false;
            }

            if (!Program.ConfigMgt.Save())
            {
                Program.Log.Write("Save config file failed. " + Program.ConfigMgt.FileName);
                Program.Log.Write(Program.ConfigMgt.LastError);

                MessageBox.Show(this, "Save config file failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (bDBChange)
            {
                if (!ScriptControl.UpdateDBConnection(
                        Program.ConfigMgt.Config.InterfaceFolder,
                        Program.ConfigMgt.Config.DataDBConnection,
                        Program.ConfigMgt.Config.ConfigDBConnection))
                {
                    Program.Log.Write("Update database connection in interfaces failed. ");
                    Program.Log.Write(GCError.LastError);

                    MessageBox.Show(this, "Update database connection in interfaces failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (MessageBox.Show(this, "Database connection will be applied after the interfaces are restarted. Do you want to restart the interfaces now?",
                                    "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    GCInterfaceManager mgt = new GCInterfaceManager(Program.ConfigDB, Program.ConfigMgt.Config.InterfaceFolder);
                    if (!mgt.RestartInterfaces())
                    {
                        Program.Log.Write("Restart interfaces failed. ");
                        Program.Log.Write(GCError.LastError);

                        MessageBox.Show(this, "Restart interfaces failed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }

            if (bOsqlChange)
            {
                string path     = Application.StartupPath + "\\..\\db_install";
                string fileName = path + "\\DropDB.bat";
                string logpath  = path + "\\temp";
                if (File.Exists(fileName))
                {
                    Program.Log.Write("Find uninstall file: " + fileName);

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("mkdir \"" + logpath + "\"");
                    sb.Append("\"" + Program.ConfigMgt.Config.OSqlFileName + "\" ");
                    sb.Append(Program.ConfigMgt.Config.OSqlParameter);
                    sb.Append(" -d master -i \"" + path + "\\DropDB.sql\"");
                    sb.Append(" > \"" + logpath + "\\GWGatewayDBUninstall.log\"");
                    string strOsql = sb.ToString();

                    using (StreamWriter sw = File.CreateText(fileName))
                    {
                        sw.Write(strOsql);
                    }

                    Program.Log.Write("Uninstall file updated. " + fileName);
                }
                else
                {
                    Program.Log.Write(LogType.Warning, "Cannot find uninstall file: " + fileName);
                }
            }

            if (bChange &&
                MessageBox.Show(this, "Configuration will be applied after the program is restarted. Do you want to restart the program now?",
                                "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Program.Restart();
            }
            else
            {
                this.Close();
            }
        }