zoukankan      html  css  js  c++  java
  • C#对Windows服务的操作

     一、安装服务:
    private void InstallService(IDictionary stateSaver, string filepath)
            {
                try
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");
                    if(!ServiceIsExisted("ServiceName"))
                    {
                        //Install Service
                        AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                        myAssemblyInstaller.UseNewContext = true;
                        myAssemblyInstaller.Path =filepath;
                        myAssemblyInstaller.Install(stateSaver);
                        myAssemblyInstaller.Commit(stateSaver);
                        myAssemblyInstaller.Dispose();
                        //--Start Service
                        service.Start();
                    }
                    else
                    {
                        if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                        {
                            service.Start();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("installServiceError
    " + ex.Message);
                }
            }
    
    二、卸载windows服务:
            private void UnInstallService(string filepath)
            {
                try
                {
                    if (ServiceIsExisted("ServiceName"))
                    {
                        //UnInstall Service
                        AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                        myAssemblyInstaller.UseNewContext = true;
                        myAssemblyInstaller.Path = filepath;
                        myAssemblyInstaller.Uninstall(null);
                        myAssemblyInstaller.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("unInstallServiceError
    " + ex.Message);
                }
            }
    
    三、判断window服务是否存在:
            private bool ServiceIsExisted(string serviceName)
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController s in services)
                {
                    if (s.ServiceName == serviceName)
                    {
                        return true;
                    }
                }
                return false;
            }
    
    四、启动服务:
    private void StartService(string serviceName)
            {
                if (ServiceIsExisted(serviceName))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                        for (int i = 0; i < 60; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                            {
                                break;
                            }
                            if (i == 59)
                            {
                                throw new Exception(startServiceError.Replace("$s$", serviceName));
                            }
                        }
                    }
                }
            }
    
    五、停止服务:
            private void StopService(string serviceName)
            {
                if (ServiceIsExisted(serviceName))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        service.Stop();
                        for (int i = 0; i < 60; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                            {
                                break;
                            }
                            if (i == 59)
                            {
                                throw new Exception(stopServiceError.Replace("$s$", serviceName));
                            }
                        }
                    }
                }
            }
    
    
    
    
    注:手动安装window服务的方法:
    
    在“Visual Studio 2005 命令提示”窗口中,运行:
    安装服务:installutil servicepath
    卸除服务:installutil /u servicepath
    
    
  • 相关阅读:
    7.5_链表_链表中添加结点
    【链表】创建新结点
    【单链表】头插法 & 尾插法
    7.5_链表_添加元素_尾插法/头插法
    7.5_链表_创建链表
    7.4_结构体_返回结构体的函数
    通俗的理解一下生成式对抗网络(GAN)
    Linux中如何让进程(或正在运行的程序)到后台运行?
    anaconda搭建本地源(加速访问),内网源(无外网访问)
    Ubuntu18.04(16和14也可以) 安装独立显卡后开机黑屏
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3369890.html
Copyright © 2011-2022 走看看