// 首次安装 dp2kernel private void MenuItem_dp2kernel_install_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; this._floatingMessage.Text = "正在安装 dp2kernel - 数据库内核 ..."; try { AppendSectionTitle("安装 dp2kernel 开始"); AppendString("正在获得可执行文件目录 ...\r\n"); Application.DoEvents(); string strExePath = InstallHelper.GetPathOfService("dp2KernelService"); if (string.IsNullOrEmpty(strExePath) == false) { strError = "dp2kernel 已经安装过了,不能重复安装"; goto ERROR1; } // strExePath = Unquote(strExePath); // program files (x86)/digitalplatform/dp2kernel string strProgramDir = GetProductDirectory("dp2kernel"); PathUtil.CreateDirIfNeed(strProgramDir); string strZipFileName = Path.Combine(this.DataDir, "kernel_app.zip"); AppendString("安装可执行文件 ...\r\n"); // 更新可执行目录 // return: // -1 出错 // 0 没有必要刷新 // 1 已经刷新 nRet = RefreshBinFiles( false, strZipFileName, strProgramDir, null, out strError); if (nRet == -1) goto ERROR1; // 创建实例 AppendString("创建实例 ...\r\n"); try { DigitalPlatform.rms.InstanceDialog dlg = new DigitalPlatform.rms.InstanceDialog(); GuiUtil.AutoSetDefaultFont(dlg); // dlg.SourceDir = strRootDir; dlg.DataZipFileName = Path.Combine(this.DataDir, "kernel_data.zip"); dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); // TODO: 是否必须要创建至少一个实例? if (dlg.DialogResult == DialogResult.Cancel) { AppendSectionTitle("放弃创建实例 ..."); return; } if (string.IsNullOrEmpty(dlg.DebugInfo) == false) AppendString("创建实例时的调试信息:\r\n" + dlg.DebugInfo + "\r\n"); if (dlg.Changed == true) { // 兑现修改 } } finally { AppendString("创建实例结束 ...\r\n"); } // 注册为 Windows Service strExePath = Path.Combine(strProgramDir, "dp2kernel.exe"); #if NO try { ManagedInstallerClass.InstallHelper(new[] { strExePath }); } catch (Exception ex) { strError = "注册 Windows Service 的过程发生错误: " + ex.Message; goto ERROR1; } #endif AppendString("注册 Windows Service ...\r\n"); nRet = InstallService(strExePath, true, out strError); if (nRet == -1) goto ERROR1; #if NO AppendString("启动 dp2kernel 服务 ...\r\n"); nRet = StartService("dp2KernelService", out strError); if (nRet == -1) goto ERROR1; AppendString("dp2kernel 服务启动成功\r\n"); #endif AppendSectionTitle("安装 dp2kernel 结束"); Refresh_dp2kernel_MenuItems(); } finally { this._floatingMessage.Text = ""; } return; ERROR1: MessageBox.Show(this, strError); }
// 卸载 dp2kernel // TODO: 可以仿照卸载 dp2library 过程重新编写。到底是先注销 Service,还是先删除数据实例 ? private void MenuItem_dp2kernel_uninstall_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; this._floatingMessage.Text = "正在卸载 dp2kernel - 数据库内核 ..."; try { AppendSectionTitle("卸载 dp2Kernel 开始"); AppendString("正在获得可执行文件目录 ...\r\n"); Application.DoEvents(); string strExePath = InstallHelper.GetPathOfService("dp2KernelService"); if (string.IsNullOrEmpty(strExePath) == true) { strError = "dp2kernel 尚未安装和注册为 Windows Service,无法进行注销"; goto ERROR1; } strExePath = StringUtil.Unquote(strExePath, "\"\""); { AppendString("正在停止 dp2kernel 服务 ...\r\n"); nRet = InstallHelper.StopService("dp2KernelService", out strError); if (nRet == -1) goto ERROR1; AppendString("dp2kernel 服务已经停止\r\n"); } #if NO DialogResult result = MessageBox.Show(this, "确实要卸载 dp2Kernel? ", "dp2Install", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.Yes) return; // cancelled #endif { DigitalPlatform.rms.InstanceDialog dlg = new DigitalPlatform.rms.InstanceDialog(); GuiUtil.AutoSetDefaultFont(dlg); dlg.Text = "dp2Kernel - 彻底卸载所有实例和数据目录"; dlg.Comment = "下列实例将被全部卸载。请仔细确认。一旦卸载,全部数据目录、数据库和实例信息将被删除,并且无法恢复。"; dlg.UninstallMode = true; dlg.SourceDir = ""; // Path.GetDirectoryName(strExePath); dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult == DialogResult.Cancel) { MessageBox.Show(this, "已放弃卸载全部实例和数据目录。仅仅卸载了执行程序。"); } else { AppendString("已删除全部数据目录\r\n"); } } // 探测 .exe 是否为新版本。新版本中 Installer.Uninstall 动作不会删除数据目录 AppendString("注销 Windows Service\r\n"); // 注销 Windows Service nRet = InstallService(strExePath, false, out strError); if (nRet == -1) goto ERROR1; AppendString("删除程序目录\r\n"); // 删除程序目录 REDO_DELETE_PROGRAMDIR: try { PathUtil.DeleteDirectory(Path.GetDirectoryName(strExePath)); } catch (Exception ex) { DialogResult temp_result = MessageBox.Show(this, "删除程序目录 '" + Path.GetDirectoryName(strExePath) + "' 出错:" + ex.Message + "\r\n\r\n是否重试?\r\n\r\n(Retry: 重试; Cancel: 不重试,继续后续卸载过程)", "卸载 dp2Kernel", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (temp_result == DialogResult.Retry) goto REDO_DELETE_PROGRAMDIR; } AppendSectionTitle("卸载 dp2Kernel 结束"); this.Refresh_dp2kernel_MenuItems(); } finally { this._floatingMessage.Text = ""; } return; ERROR1: MessageBox.Show(this, strError); }
private void MenuItem_dp2kernel_instanceManagement_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; bool bControl = Control.ModifierKeys == Keys.Control; bool bInstalled = true; this._floatingMessage.Text = "正在配置 dp2kernel 实例 ..."; try { AppendSectionTitle("配置实例开始"); AppendString("正在获得可执行文件目录 ...\r\n"); Application.DoEvents(); string strExePath = InstallHelper.GetPathOfService("dp2KernelService"); if (string.IsNullOrEmpty(strExePath) == true) { if (bControl == false) { strError = "dp2kernel 未曾安装过"; goto ERROR1; } bInstalled = false; } //strExePath = Unquote(strExePath); //string strRootDir = Path.GetDirectoryName(strExePath); // MessageBox.Show(this, "为进行配置,将首先停止 dp2library 服务。配置完成后 dp2library 会重新启动"); if (bInstalled == true) { AppendString("正在停止 dp2kernel 服务 ...\r\n"); nRet = InstallHelper.StopService("dp2KernelService", out strError); if (nRet == -1) goto ERROR1; AppendString("dp2kernel 服务已经停止\r\n"); } try { DigitalPlatform.rms.InstanceDialog dlg = new DigitalPlatform.rms.InstanceDialog(); GuiUtil.AutoSetDefaultFont(dlg); // dlg.SourceDir = strRootDir; dlg.DataZipFileName = Path.Combine(this.DataDir, "kernel_data.zip"); dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult == DialogResult.Cancel) return; if (string.IsNullOrEmpty(dlg.DebugInfo) == false) AppendString("管理实例时的调试信息:\r\n" + dlg.DebugInfo + "\r\n"); if (dlg.Changed == true) { // 兑现修改 } } finally { if (bInstalled == true) { string strError1 = ""; AppendString("正在重新启动 dp2kernel 服务 ...\r\n"); nRet = InstallHelper.StartService("dp2KernelService", out strError1); if (nRet == -1) { AppendString("dp2kernel 服务启动失败: " + strError1 + "\r\n"); MessageBox.Show(this, strError1); } else { AppendString("dp2kernel 服务启动成功\r\n"); } } AppendSectionTitle("配置实例结束"); } } finally { this._floatingMessage.Text = ""; } return; ERROR1: AppendString("出错: " + strError + "\r\n"); MessageBox.Show(this, strError); }