使用工具安裝,運行,中止,卸載Window服務

 

 

WSWinForm.exe介紹

      WSWinForm.exe是我本身開發的一個實用的小工具,用於將任何EXE程序做爲Windows服務運行。也就是說WSWinForm只是其註冊程序的服務外殼,這個特性對於咱們來講很是實用,咱們能夠經過它來安裝,運行,中止,卸載Windows服務,而再也不是經過命令行InstallUtil的方式來安裝。javascript

資源下載

      你能夠經過本文下載。html

  應用程序java

  源代碼git

  最新版本信息查看github

  GitHub地址:https://github.com/CrazyJson/TaskManageride

  SVN地址:http://code.taobao.org/svn/TaskManagerPub/Branchsvn

如何使用

      下載完軟件之後,咱們能幹些什麼呢?看看這個截圖吧:。工具

 

這裏能夠看到的操做:spa

1. 安裝指定路徑的服務,命令行

2. 運行指定服務,

3. 中止正在運行的服務,

4. 卸載服務,

這些功能是怎麼經過代碼來實現的呢,我後面再說。先對它有個印象就能夠了。

代碼解析

1.安裝功能:

 1                 string[] cmdline = { };
 2                 string serviceFileName = txtPath.Text.Trim();
 3                 string serviceName = GetServiceName(serviceFileName);
 4                 if (string.IsNullOrEmpty(serviceName))
 5                 {
 6                     txtTip.Text = "指定文件不是Windows服務!";
 7                     return;
 8                 }
 9                 if (ServiceIsExisted(serviceName))
10                 {
11                     txtTip.Text = "要安裝的服務已經存在!";
12                     return;
13                 }
14                 TransactedInstaller transactedInstaller = new TransactedInstaller();
15                 AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
16                 transactedInstaller.Installers.Add(assemblyInstaller);
17                 transactedInstaller.Install(new System.Collections.Hashtable());
18                 txtTip.Text = "服務安裝成功!";            
View Code

 上面這段代碼中最爲中要的部分是方法 GetServiceName,經過給定路徑獲取服務的名稱。下面來看看這個方法是怎麼實現的。

 1  /// <summary>
 2         /// 獲取Windows服務的名稱
 3         /// </summary>
 4         /// <param name="serviceFileName">文件路徑</param>
 5         /// <returns>服務名稱</returns>
 6         private string GetServiceName(string serviceFileName)
 7         {
 8             try
 9             {
10                 Assembly assembly = Assembly.LoadFrom(serviceFileName);
11                 Type[] types = assembly.GetTypes();
12                 foreach (Type myType in types)
13                 {
14                     if (myType.IsClass && myType.BaseType == typeof(System.Configuration.Install.Installer))
15                     {
16                         FieldInfo[] fieldInfos = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default | BindingFlags.Instance | BindingFlags.Static);
17                         foreach (FieldInfo myFieldInfo in fieldInfos)
18                         {
19                             if (myFieldInfo.FieldType == typeof(System.ServiceProcess.ServiceInstaller))
20                             {
21                                 ServiceInstaller serviceInstaller = (ServiceInstaller)myFieldInfo.GetValue(Activator.CreateInstance(myType));
22                                 return serviceInstaller.ServiceName;
23                             }
24                         }
25                     }
26                 }
27                 return "";
28             }
29             catch (Exception ex)
30             {
31                 throw ex;
32             }
33         }
View Code

 1.加載程序集

 2.獲取程序集裏面繼承於System.Configuration.Install.Installer這個類的類,緣由在於Windows服務都須要添加一個安裝程序,而安裝程序是繼承這個類的

 安裝之後的服務名稱是經過這個類ServiceInstaller的變量指定的,好比ServiceInstaller.ServiceName = "xxx";

 3.獲取第二步Installer類裏面的ServiceInstaller變量的值,而後獲取這個值的ServiceName屬性就是服務的名稱。

 2.運行功能:

 1 try
 2             {
 3                 string serviceName = GetServiceName(txtPath.Text.Trim());
 4                 if (string.IsNullOrEmpty(serviceName))
 5                 {
 6                     txtTip.Text = "指定文件不是Windows服務!";
 7                     return;
 8                 }
 9                 if (!ServiceIsExisted(serviceName))
10                 {
11                     txtTip.Text = "要運行的服務不存在!";
12                     return;
13                 }
14                 ServiceController service = new ServiceController(serviceName);
15                 if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
16                 {
17                     service.Start();
18                     txtTip.Text = "服務運行成功!";
19                 }
20                 else
21                 {
22                     txtTip.Text = "服務正在運行!";
23                 }
24             }
25             catch (Exception ex)
26             {
27                 txtTip.Text = ex.InnerException.ToString();
28             }
View Code

重要的是ServiceController這個類,這個類能夠獲取系統中全部的服務

 1         /// <summary>
 2         /// 判斷服務是否已經存在
 3      /// </summary>
 4         /// <param name="serviceName">服務名稱</param>
 5         /// <returns>bool</returns>
 6         private bool ServiceIsExisted(string serviceName)
 7         {
 8             ServiceController[] services = ServiceController.GetServices();
 9             foreach (ServiceController s in services)
10             {
11                 if (s.ServiceName == serviceName)
12                 {
13                     return true;
14                 }
15             }
16             return false;
17         }
View Code

 3.中止功能:

 1 ry
 2             {
 3                 string[] cmdline = { };
 4                 string serviceFileName = txtPath.Text.Trim();
 5                 string serviceName = GetServiceName(serviceFileName);
 6                 if (string.IsNullOrEmpty(serviceName))
 7                 {
 8                     txtTip.Text = "指定文件不是Windows服務!";
 9                     return;
10                 }
11                 if (!ServiceIsExisted(serviceName))
12                 {
13                     txtTip.Text = "要中止的服務不存在!";
14                     return;
15                 }
16                 ServiceController service = new ServiceController(serviceName);
17                 if (service.Status == ServiceControllerStatus.Running)
18                 {
19                     service.Stop();
20                     txtTip.Text = "服務中止成功!";
21                 }
22                 else
23                 {
24                     txtTip.Text = "服務已經中止!";
25                 }
26 
27             }
28             catch (Exception ex)
29             {
30                 txtTip.Text = ex.InnerException.ToString();
31             }
View Code

4.卸載功能:

 1  try
 2             {
 3                 string[] cmdline = { };
 4                 string serviceFileName = txtPath.Text.Trim();
 5                 string serviceName = GetServiceName(serviceFileName);
 6                 if (string.IsNullOrEmpty(serviceName))
 7                 {
 8                     txtTip.Text = "指定文件不是Windows服務!";
 9                     return;
10                 }
11                 if (!ServiceIsExisted(serviceName))
12                 {
13                     txtTip.Text = "要卸載的服務不存在!";
14                     return;
15                 }
16                 TransactedInstaller transactedInstaller = new TransactedInstaller();
17                 AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
18                 transactedInstaller.Installers.Add(assemblyInstaller);
19                 transactedInstaller.Uninstall(null);
20                 txtTip.Text = "服務卸載成功!";
21 
22             }
23             catch (Exception ex)
24             {
25                 txtTip.Text = ex.InnerException.ToString();
26             }
View Code

 

總結

1.總體來講實現了服務的整個功能,能夠方便的運行中止服務,而再也不是使用命令行的方式。

2.下一篇將講解,使用Windows服務實現任務處理(及定時執行某個功能)。

 



原文連接:http://www.cnblogs.com/yanweidie/p/3542670.html

相關文章
相關標籤/搜索