zoukankan      html  css  js  c++  java
  • C#开发Windows服务的基础代码

    做项目需要对Windows服务进行操作,从网上找了一些资料,总结如下:

    (以下程序在程序中测试通过)

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Text;
      7 using System.Windows.Forms;
      8 using System.Collections;
      9 using System.Configuration.Install;
     10 using System.Collections.Specialized;
     11 using System.ServiceProcess;
     12 
     13 namespace IAU.ServerInstall.BLL
     14 {
     15     public class ServiceControl
     16     {
     17         /// <summary>
     18         /// 注册服务(注册完就启动,已经存在的服务直接启动。)
     19         /// </summary>
     20         /// <param name="strServiceName">服务名称</param>
     21         /// <param name="strServiceInstallPath">服务安装程序完整路径(.exe程序完整路径)</param>
     22         public void Register(string strServiceName, string strServiceInstallPath)
     23         {
     24             IDictionary mySavedState = new Hashtable();
     25 
     26             try
     27             {
     28                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(strServiceName);
     29 
     30                 //服务已经存在则卸载
     31                 if (ServiceIsExisted(strServiceName))
     32                 {
     33                     //StopService(strServiceName);
     34                     UnInstallService(strServiceName, strServiceInstallPath);
     35                 }
     36                 service.Refresh();
     37                 //注册服务 http://www.cnblogs.com/sosoft/
     38                 AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
     39 
     40                 mySavedState.Clear();
     41                 myAssemblyInstaller.Path = strServiceInstallPath;
     42                 myAssemblyInstaller.UseNewContext = true;
     43                 myAssemblyInstaller.Install(mySavedState);
     44                 myAssemblyInstaller.Commit(mySavedState);
     45                 myAssemblyInstaller.Dispose();
     46 
     47                 service.Start();
     48             }
     49             catch (Exception ex)
     50             {
     51                 throw new Exception("注册服务时出错:" + ex.Message);
     52             }
     53         }
     54 
     55         /// <summary>
     56         /// 卸载服务
     57         /// </summary>
     58         /// <param name="strServiceName">服务名称</param>
     59         /// <param name="strServiceInstallPath">服务安装程序完整路径(.exe程序完整路径)</param>
     60         public void UnInstallService(string strServiceName, string strServiceInstallPath)
     61         {
     62             try
     63             {
     64                 if (ServiceIsExisted(strServiceName))
     65                 {
     66                     //UnInstall Service
     67                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
     68                     myAssemblyInstaller.UseNewContext = true;
     69                     myAssemblyInstaller.Path = strServiceInstallPath;
     70                     myAssemblyInstaller.Uninstall(null);
     71                     myAssemblyInstaller.Dispose();
     72                 }
     73             }
     74             catch (Exception ex)
     75             {
     76                 throw new Exception("卸载服务时出错:" + ex.Message);
     77             }
     78         }
     79 
     80 
     81         /// <summary>
     82         /// 判断服务是否存在
     83         /// </summary>
     84         /// <param name="serviceName">服务名</param>
     85         /// <returns></returns>
     86         public bool ServiceIsExisted(string serviceName)
     87         {
     88             ServiceController[] services = ServiceController.GetServices();
     89             foreach (ServiceController s in services)
     90             {
     91                 if (s.ServiceName == serviceName)
     92                 {
     93                     return true;
     94                 }
     95             }
     96             return false;
     97         }
     98 
     99         /// <summary>
    100         /// 启动服务(启动存在的服务,30秒后启动失败报错)
    101         /// </summary>
    102         /// <param name="serviceName">服务名</param>
    103         public void StartService(string serviceName)
    104         {
    105             if (ServiceIsExisted(serviceName))
    106             {
    107                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
    108                 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
    109                 {
    110                     service.Start();
    111                     for (int i = 0; i < 30; i++)
    112                     {
    113                         service.Refresh();
    114                         System.Threading.Thread.Sleep(1000);
    115                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
    116                         {
    117                             break;
    118                         }
    119                         if (i == 29)
    120                         {
    121                             throw new Exception("服务" + serviceName + "启动失败!");
    122                         }
    123                     }
    124                 }
    125             }
    126         }
    127 
    128         /// <summary>
    129         /// 停止服务(停止存在的服务,30秒后停止失败报错)
    130         /// </summary>
    131         /// <param name="serviceName"></param>
    132         public void StopService(string serviceName)
    133         {
    134             if (ServiceIsExisted(serviceName))
    135             {
    136                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
    137                 if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
    138                 {
    139                     service.Stop();
    140                     for (int i = 0; i < 30; i++)
    141                     {
    142                         service.Refresh();
    143                         System.Threading.Thread.Sleep(1000);
    144                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
    145                         {
    146                             break;
    147                         }
    148                         if (i == 29)
    149                         {
    150                             throw new Exception("服务" + serviceName + "停止失败!");
    151                         }
    152                     }
    153                 }
    154             }
    155         }
    156     }
    157 }

    博客园 柔城  以上代码概述:

    C#对Windows服务操作(注册安装服务,卸载服务,启动停止服务,判断服务存在)

  • 相关阅读:
    spring 事务
    Servlet详解之两个init方法的作用
    被request.getLocalAddr()苦闷了很久
    Java获取IP地址:request.getRemoteAddr()警惕
    MongoDB笔记
    hexo+github搭建博客
    Python处理Excel(使用openpyxl库)
    Wireshark使用学习
    查看开启操作系统端口
    记录Centos7服务器搭建过程
  • 原文地址:https://www.cnblogs.com/sosoft/p/3551033.html
Copyright © 2011-2022 走看看