zoukankan      html  css  js  c++  java
  • c# Windows服务管理

    .NET Framework中提供的类库可以很方便的实现对windows服务的安装、卸载、启动、停止、获取运行状态等功能。这些类都在System.ServiceProcess命名空间下。

    所以,在开始编写程序之前,需要先引用System.ServiceProcess。

    获取Windows服务列表:

    // 获取服务列表
    ServiceController[] serviceList = ServiceController.GetServices();
    // 按名称排序
    serviceList = serviceList.OrderBy(m => m.DisplayName).ToArray();
    // 遍历服务列表
    foreach (ServiceController sc in serviceList)
    {
        // 服务信息
    }

    启动服务:

    string serviceName="服务名称";
    ServiceController sc = new ServiceController(serviceName); //建立服务对象
    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
    {
        sc.Start();
        sc.WaitForStatus(ServiceControllerStatus.Running); //等待启动
        sc.Refresh();
    }

    停止服务:

    string serverName="服务名称";
    ServiceController sc = new ServiceController(serviceName); //建立服务对象
    if (sc.Status.Equals(ServiceControllerStatus.Running))
    {
        sc.Stop();
        sc.WaitForStatus(ServiceControllerStatus.Stopped);  //等待停止
        sc.Refresh();
    }

    重启服务:

    string serviceName = "服务名称";
    ServiceController sc = new ServiceController(serviceName); //建立服务对象
    if (sc.Status.Equals(ServiceControllerStatus.Running))
    {
        sc.Stop();
        sc.WaitForStatus(ServiceControllerStatus.Stopped);  //等待停止
        sc.Refresh();
    }
    sc.Start();
    sc.WaitForStatus(ServiceControllerStatus.Running); //等待启动
    sc.Refresh();
  • 相关阅读:
    Nginx 413 Request Entity Too Large
    Docker 搭建一个多端同步网盘-Nextcloud
    FTP主动模式(PORT)与被动模式(PASV)
    Linux安装与使用FTP服务-vsftpd
    Mac FTP工具推荐-Transmit
    Nginx配置动静分离
    Nginx解决跨域问题(CORS)
    CentOS 7 之找回失落的ifconfig
    CentOS 7 之安装篇
    深入研究MiniMVC之后续篇
  • 原文地址:https://www.cnblogs.com/bossing/p/11341368.html
Copyright © 2011-2022 走看看