C# 创建window服务

来源:互联网 发布:james blunt 知乎 编辑:程序博客网 时间:2024/06/14 15:43

以前写的后台程序都是用控制台的,刚才尝试了一下这个生成服务的程序,很强大,转载过来,大家看看!

 

 

  1. using System;  
  2. using System.Threading;  
  3. using System.ServiceProcess;  
  4. using System.Collections;  
  5. using System.Configuration.Install;  
  6. using System.Diagnostics;  
  7. using System.Collections.Generic;  
  8. using System.ComponentModel;  
  9. using System.Data;  
  10. using System.Drawing;  
  11. using System.Text;  
  12. using System.Windows.Forms;  
  13. using System.Collections.Specialized;  
  14. using System.IO;  
  15. namespace WSGPSServices  
  16. {  
  17.     public partial class myService : ServiceBase  
  18.     {  
  19.         #region 成员变量  
  20.         public string strCurrPath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory.FullName;  
  21.         public string servicesname = "WSGPSServices";//服务    
  22.         public string Proceename = "WSGPSGateway.exe";// 进程    
  23.  
  24.         public System.ComponentModel.Container components;  
  25.         System.Windows.Forms.Timer Timers;  
  26.         #endregion  
  27.         protected override void Dispose(bool disposeing)  
  28.         {  
  29.             if (disposeing)  
  30.             {  
  31.                 if (components != null)  
  32.                 {  
  33.                     components.Dispose();  
  34.                 }  
  35.                 Register(servicesname, strCurrPath);  
  36.             }  
  37.         }  
  38.         #region 注册卸载服务  
  39.         //注册服务    
  40.         public void Register(string servicesname, string strServiceInstallPath)  
  41.         {  
  42.             IDictionary mySavedState = new Hashtable();  
  43.             try 
  44.             {  
  45.                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(servicesname);  
  46.                 //服务已经存在则卸载      
  47.                 if (ServiceIsExisted(servicesname))  
  48.                 {  
  49.                     UnInstallService(servicesname, strCurrPath);  
  50.                 }  
  51.                 service.Refresh();  
  52.                 //注册服务      
  53.                 AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();  
  54.                 mySavedState.Clear();  
  55.                 myAssemblyInstaller.Path = "WSGPSServices.exe";  
  56.                 myAssemblyInstaller.UseNewContext = true;  
  57.                 myAssemblyInstaller.Install(mySavedState);  
  58.                 myAssemblyInstaller.Commit(mySavedState);  
  59.                 myAssemblyInstaller.Dispose();  
  60.                 service.Start();  
  61.                 ProcessStartInfo startinfo = new ProcessStartInfo("WSGPSGateway.exe");  
  62.                 startinfo.WindowStyle = ProcessWindowStyle.Hidden;  
  63.                 startinfo.Arguments = Application.StartupPath + "//WSGPSGateway.exe";  
  64.                 Process.Start(startinfo);  
  65.                 InitializeComponent();  
  66.             }  
  67.             catch (Exception ex)  
  68.             {  
  69.                 throw new Exception("注册服务时出错:" + ex.Message);  
  70.             }  
  71.         }  
  72.         //卸载服务    
  73.         public void UnInstallService(string strServiceName, string strServiceInstallPath)  
  74.         {  
  75.             try 
  76.             {  
  77.                 if (ServiceIsExisted(servicesname))  
  78.                 {  
  79.                     AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();  
  80.                     myAssemblyInstaller.UseNewContext = true;  
  81.                     myAssemblyInstaller.Path = "WSGPSServices.exe";  
  82.                     myAssemblyInstaller.Uninstall(null);  
  83.                     myAssemblyInstaller.Dispose();  
  84.                     KillProcess();  
  85.                 }  
  86.             }  
  87.             catch (Exception ex)  
  88.             {  
  89.                 throw new Exception("卸载服务时出错:" + ex.Message);  
  90.             }  
  91.         }  
  92.         //卸载服务时结束进程    
  93.         private void KillProcess()  
  94.         {  
  95.             System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程         
  96.             try 
  97.             {  
  98.                 foreach (Process thisproc in Process.GetProcessesByName(Proceename))  
  99.                 {  
  100.                     if (!thisproc.CloseMainWindow())  
  101.                     {  
  102.                         thisproc.Kill();  
  103.                     }  
  104.                 }  
  105.             }  
  106.             catch (Exception Exc)  
  107.             {  
  108.                 throw Exc;  
  109.             }  
  110.         }  
  111.         #endregion  
  112.         #region 启动服务与应用程序  
  113.         //启动服务(启动存在的服务,30秒后启动失败报错)      
  114.         public void StartService(string serviceName)  
  115.         {  
  116.             if (ServiceIsExisted(servicesname))  
  117.             {  
  118.                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("WSGPSServices");  
  119.                 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)  
  120.                 {  
  121.                     service.Start();  
  122.                     for (int i = 0; i < 30; i++)  
  123.                     {  
  124.                         service.Refresh();  
  125.                         System.Threading.Thread.Sleep(1000);  
  126.                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)  
  127.                         {  
  128.                             ProcessStartInfo startinfo = new ProcessStartInfo("WSGPSGateway.exe");  
  129.                             startinfo.WindowStyle = ProcessWindowStyle.Hidden;  
  130.                             startinfo.Arguments = Application.StartupPath + "//WSGPSGateway.exe";  
  131.                             Process.Start(startinfo);  
  132.                             break;  
  133.                         }  
  134.                         if (i == 29)  
  135.                         {  
  136.                             throw new Exception("服务WSGPS Services启动失败!");  
  137.                         }  
  138.                     }  
  139.                 }  
  140.             }  
  141.         }  
  142.         //判断服务是否存在      
  143.         public bool ServiceIsExisted(string serviceName)  
  144.         {  
  145.             ServiceController[] services = ServiceController.GetServices();  
  146.             foreach (ServiceController s in services)  
  147.             {  
  148.                 if (s.ServiceName == servicesname)  
  149.                 {  
  150.                     return true;  
  151.                 }  
  152.             }  
  153.             return false;  
  154.         }  
  155.         #endregion  
  156.         #region Timer定时任务与构造方法  
  157.         //Timer构造方法    
  158.         private void InitializeComponent()  
  159.         {  
  160.             this.components = new System.ComponentModel.Container();  
  161.             this.Timers = new System.Windows.Forms.Timer(this.components);  
  162.             this.Timers.Enabled = true;  
  163.             this.Timers.Interval = 180000;  
  164.             this.Timers.Tick += new System.EventHandler(this.Timers_Tick);  
  165.         }  
  166.         //Timer定时检查任务    
  167.         private void Timers_Tick(object sender, EventArgs e)  
  168.         {  
  169.             ServiceController[] services = ServiceController.GetServices();  
  170.             foreach (ServiceController s in services)  
  171.             {  
  172.                 if (s.ServiceName == servicesname)  
  173.                 {  
  174.                     return;//存在返回空    
  175.                 }  
  176.                 else 
  177.                 {  
  178.                     Register(servicesname, strCurrPath);//无当前服务则重新注册服务    
  179.                 }  
  180.             }  
  181.         }  
  182.         #endregion  
  183.     }  
原创粉丝点击