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


 

WSWinForm.exe介紹

      WSWinForm.exe是我自己開發的一個實用的小工具,用於將任何EXE程序作為Windows服務運行。也就是說WSWinForm只是其注冊程序的服務外殼,這個特性對於我們來說非常實用,我們可以通過它來安裝,運行,停止,卸載Windows服務,而不再是通過命令行InstallUtil的方式來安裝。

資源下載

      你可以通過本文下載。

  應用程序

  源代碼

  最新版本信息查看

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

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

如何使用

      下載完軟件以后,我們能干些什么呢?看看這個截圖吧:。

 

這里可以看到的操作:

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服務實現任務處理(及定時執行某個功能)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM