zoukankan      html  css  js  c++  java
  • C# 操作windows服务[启动、停止、卸载、安装]

    主要宗旨:不已命令形式操作windows服务

          static void Main(string[] args)
            {
                var path = @"E:开发辅助项目WCFWCF.TestWCF.TestinDebugWCF.Test.exe";
                var serviceName = "WCF_TestService";
                HandleService hs = new HandleService();
                //启动服务
                hs.StartService(serviceName);
                //停止服务
                hs.StopService(serviceName);
                //卸载服务
                hs.DeleteService(path);
                //安装服务
                hs.InstallService(path);
                Console.ReadKey();
            }

    操作类

     public class HandleService
        {
    
            /// <summary>
            /// 获取服务
            /// </summary>
            /// <param name="serviceName">服务名</param>
            /// <returns></returns>
            public ServiceController GetService(string serviceName)
            {
                try
                {
                    //可以获取所有的服务
                    //ServiceController[] service = ServiceController.GetServices();
    
                    //无法打开计算上的服务控制管理器。
                    //ServiceController[] service = ServiceController.GetServices(serviceName);
                    //if (service.Length <= 0) return null;
    
                    ServiceController service = new ServiceController(serviceName);
                    return service;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 安装服务
            /// </summary>
            /// <param name="path">路径</param>
            public void InstallService(string path)
            {
                try
                {
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path = path;
                    myAssemblyInstaller.Install(null);
                    myAssemblyInstaller.Commit(null);
                    myAssemblyInstaller.Dispose();
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.Read();
            }
    
            /// <summary>
            /// 启动服务
            /// </summary>
            /// <param name="serviceName">服务名</param>
            public void StartService(string serviceName)
            {
                try
                {
                    var service = GetService(serviceName);
                    if (service == null)
                    {
                        Console.Write($"windows上不存在{serviceName}服务!");
                        return;
                    }
                    if (service.Status == ServiceControllerStatus.Paused ||
                        service.Status == ServiceControllerStatus.Stopped)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                    }
                    Console.WriteLine($"{serviceName}已经启动");
                }
                catch (Exception)
                {
    
                    throw;
                }
            }
    
            /// <summary>
            /// 停止服务
            /// </summary>
            /// <param name="serviceName">服务名</param>
            public void StopService(string serviceName)
            {
                try
                {
                    var service = GetService(serviceName);
                    if (service == null)
                    {
                        Console.Write($"windows上不存在{serviceName}服务!");
                        return;
                    }
                    if (service.Status == ServiceControllerStatus.Running ||
                        service.Status == ServiceControllerStatus.PausePending || service.Status == ServiceControllerStatus.StartPending)
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
                    }
                    Console.WriteLine($"{serviceName}已经停止");
                }
                catch (Exception)
                {
    
                    throw;
                }
            }
    
            /// <summary>
            /// 卸载服务
            /// </summary>
            /// <param name="path"></param>
            public void DeleteService(string path)
            {
                try
                {
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path = path;
                    myAssemblyInstaller.Uninstall(null);
                    myAssemblyInstaller.Dispose();
                    Console.WriteLine("卸载服务成功!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("卸载服务失败");
                }
    
            }
    
        }
  • 相关阅读:
    信号量的实现
    锁的实现
    Do PDB Files Affect Performance?
    分布式系统一致性算法(Raft)
    idea开发工具破解地址
    IOS设备上传图片,使用ImageIO.write 图片翻转纠正(JAVA)
    使用WebSocket进行消息推送(附代码)
    SpringCloud构建微服务 | 服务注册与发现(一)提供Demo
    @slf4j注解找不到log变量-已解决
    springBoot事务无法回滚 MyISAM与InnoDB区别
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/6566200.html
Copyright © 2011-2022 走看看