Exemplo n.º 1
0
        /// <summary>
        /// 卸载window服务
        /// </summary>
        public void UninstallService()
        {
            if (InstallerConfig == null)
            {
                Console.WriteLine("无效的服务配置项!");
                return;
            }

            ServiceController controller = InstallerUtils.LookupService(InstallerConfig.ServiceName);

            if (controller != null)
            {
                try
                {
                    TransactedInstaller installer = GetTransactedInstaller();
                    installer.Uninstall(null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("服务{0}尚未安装,输入'/?'查看帮助!", InstallerConfig.ServiceName);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 安装服务器为window服务
        /// </summary>
        public void InstallService()
        {
            if (InstallerConfig == null)
            {
                Console.WriteLine("无效的服务配置项!");
                return;
            }

            ServiceController controller = InstallerUtils.LookupService(InstallerConfig.ServiceName);

            if (controller == null)
            {
                try
                {
                    TransactedInstaller installer = GetTransactedInstaller();
                    installer.Install(new Hashtable());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("服务{0}已经存在,请先卸载后再执行此命令!", InstallerConfig.ServiceName);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 从控制台运行
        /// </summary>
        public bool StartConsole()
        {
            if (InstallerConfig == null)
            {
                Console.WriteLine("无效的服务配置项!");
                return(false);
            }

            ServiceController controller = InstallerUtils.LookupService(InstallerConfig.ServiceName);

            if (controller != null)
            {
                if (controller.Status == ServiceControllerStatus.Running)
                {
                    Console.WriteLine("服务已经启动,不能从控制台启动,请先停止服务后再执行该命令!");
                    return(false);
                }
            }

            if (WindowsService != null)
            {
                WindowsService.StartMode = StartMode.Console;
                WindowsService.Start();

                Console.WriteLine("控制台已经启动......");
                return(true);
            }
            else
            {
                Console.WriteLine("无效的服务启动项!");
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 停止服务服务
        /// </summary>
        public void StopService(string serviceName)
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                if (InstallerConfig == null)
                {
                    Console.WriteLine("无效的服务配置项!");
                    return;
                }

                serviceName = InstallerConfig.ServiceName;
            }

            if (string.IsNullOrEmpty(serviceName))
            {
                Console.WriteLine("无效的服务名称!");
                return;
            }

            ServiceController controller = InstallerUtils.LookupService(serviceName);

            if (controller != null)
            {
                if (controller.Status == ServiceControllerStatus.Running)
                {
                    Console.WriteLine("正在停止服务{0}.....", serviceName);
                    try
                    {
                        controller.Stop();
                        controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
                        controller.Refresh();

                        if (controller.Status == ServiceControllerStatus.Stopped)
                        {
                            Console.WriteLine("停止服务{0}成功!", serviceName);
                        }
                        else
                        {
                            Console.WriteLine("停止服务{0}失败!", serviceName);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    Console.WriteLine("服务{0}已经停止!", serviceName);
                }
            }
            else
            {
                Console.WriteLine("不存在服务{0}!", serviceName);
            }
        }