• 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("卸载服务失败");
                }
    
            }
    
        }
  • 相关阅读:
    转:jQuery选择器大全(48个代码片段+21幅图演示)
    转:Web 开发中很实用的10个效果【附源码下载】
    转:在网站开发中很有用的8个 jQuery 效果【附源码】
    转:35个让人惊讶的 CSS3 动画效果演示
    转:总结const、readonly、static三者的区别
    转:C# 深入理解堆栈、堆在内存中的实现
    推荐:Asp.Net MVC 多语言(html+js共用一套资源文件)
    转:HttpModule与HttpHandler详解
    转: ASP.NET MVC 多语言配置
    spring集合类型注入
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/6566200.html
Copyright © 2020-2023  润新知