c#编写一个winform 管理一个windows服务

使用c#编写一个winform程序, 管理一个windows服务。对指定的windows服务,进行启动、停止、重新启动

1、引入 using System.ServiceProcess;

2、

/// <summary>
      /// 重新启动按钮
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void btnStopAndRun_Click(object sender, EventArgs e)
      {
          ServiceController serviceObject = this.GetServiceObject();
          try
          {
              this.ReStarService(serviceObject);
              MessageBox.Show("重启服务成功!");
          }
          catch (Exception ex)
          {
              MessageBox.Show("重启服务错误:" + ex.Message.ToString());
          }
      }
 
      //获取windows服务实例
      private ServiceController GetServiceObject()
      {
          return new ServiceController("FloatcarGpsAppServer");
      }
      /// <summary>
      /// 重启
      /// </summary>
      /// <param name="sc"></param>
      private void ReStarService(ServiceController sc)
      {
          this.StopService(sc);
          this.StarService(sc);
      }
      /// <summary>
      ///  停止服务
      /// </summary>
      /// <param name="sc"></param>
      private void StopService(ServiceController sc)
      {
          if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Running))
          {
              sc.Stop();
          }
      }
      /// <summary>
      /// 开始服务
      /// </summary>
      /// <param name="sc"></param>
      private void StarService(ServiceController sc)
      {
          if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Stopped))
          {
              sc.Start();
          }
      }
      /// <summary>
      /// 停止服务按钮
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void btnStop_Click(object sender, EventArgs e)
      {
          ServiceController serviceObject = this.GetServiceObject();
          try
          {
              this.StopService(serviceObject);
              MessageBox.Show("停止服务成功!");
          }
          catch (Exception ex)
          {
 
              MessageBox.Show("停止服务发生错误:" + ex.Message.ToString());
          }
      }
      /// <summary>
      /// 开始服务按钮
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void btnRun_Click(object sender, EventArgs e)
      {
          ServiceController serviceObject = this.GetServiceObject();
          try
          {
              this.StarService(serviceObject);
              MessageBox.Show("启动服务成功!");
          }
          catch (Exception  ex)
          {
 
              MessageBox.Show("启动服务失败:" + ex.Message);
          }
      }
posted @   走路到纽约  阅读(2301)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
点击右上角即可分享
微信分享提示