Beispiel #1
0
        public override void RefreshStatus()
        {
            try
            {
                serviceController.Refresh();

                if (serviceController.Status == ServiceControllerStatus.Running)
                {
                    UpdateServiceStatus(HostedServiceStatus.Running);
                }
                else if (serviceController.Status == ServiceControllerStatus.Stopped)
                {
                    UpdateServiceStatus(HostedServiceStatus.Stopped);
                }

                bool        delayedAutoStart;
                ServiceInfo serviceInfo = ServiceHelper.QueryServiceConfig(serviceController.ServiceName, out delayedAutoStart);
                if (serviceInfo.startType == 2 && delayedAutoStart)
                {
                    cbStartupType.SelectedIndex = 0;
                }
                else if (serviceInfo.startType == 2 && !delayedAutoStart)
                {
                    cbStartupType.SelectedIndex = 1;
                }
                else if (serviceInfo.startType == 3)
                {
                    cbStartupType.SelectedIndex = 2;
                }
                else if (serviceInfo.startType == 4)
                {
                    cbStartupType.SelectedIndex = 3;
                }
            }
            catch (Exception ex)
            {
                logError("刷新服务状态出错:" + ex.Message, ex);
            }
        }
Beispiel #2
0
        private void bInstall_Click(object sender, EventArgs e)
        {
            try
            {
                if (containNotInstall) //安装
                {
                    if (ShowConfirm("如果您之前执行过安装操作,请先使用原程序卸载服务再使用本程序执行安装操作!"))
                    {
                        using (TransactedInstaller transactedInstaller = new TransactedInstaller())
                        {
                            var installLog = new Hashtable();
                            AssemblyInstaller assemblyInstaller = new AssemblyInstaller(Application.ExecutablePath, null);
                            transactedInstaller.Installers.Add(assemblyInstaller);
                            transactedInstaller.Install(installLog);
                        }

                        //修改ImagePath
                        foreach (var service in GetServicesFromMef())
                        {
                            if (service is WindowsServiceBase)
                            {
                                bool        delayedAutoStart;
                                ServiceInfo serviceInfo = ServiceHelper.QueryServiceConfig(service.ServiceName, out delayedAutoStart);
                                ServiceHelper.ChangeExePath(service.ServiceName, $"{serviceInfo.binaryPathName} -Daemon {service.ServiceName}");
                            }
                        }
                        updateInstallButton();

                        logInfo("安装所有Windows服务成功");
                    }
                }
                else //卸载
                {
                    if (ShowConfirm("确定要卸载本控制台中的所有Windows服务吗?"))
                    {
                        using (TransactedInstaller transactedInstaller = new TransactedInstaller())
                        {
                            AssemblyInstaller assemblyInstaller = new AssemblyInstaller(Application.ExecutablePath, null);
                            transactedInstaller.Installers.Add(assemblyInstaller);
                            transactedInstaller.Uninstall(null);
                        }
                        updateInstallButton();

                        logInfo("卸载所有Windows服务成功");
                    }
                }
            }
            catch (Exception ex)
            {
                logError("操作失败:" + ex.Message, ex);
                if (ShowConfirm($"操作失败【{ex.Message}】,是否以管理员权限重启本程序以执行操作?"))
                {
                    //退出
                    tsmiExit.PerformClick();

                    //创建启动对象
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    startInfo.UseShellExecute  = true;
                    startInfo.WorkingDirectory = Environment.CurrentDirectory;
                    startInfo.FileName         = Application.ExecutablePath;
                    //设置启动动作,确保以管理员身份运行
                    startInfo.Verb = "runas";
                    try
                    {
                        System.Diagnostics.Process.Start(startInfo);
                    }
                    catch { }
                }
            }
        }