国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用工具安裝,運(yùn)行,停止,卸載Window服務(wù)

2019-11-17 03:22:05
字體:
供稿:網(wǎng)友

使用工具安裝,運(yùn)行,停止,卸載Window服務(wù)

WSWinForm.exe介紹

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

資源下載

你可以通過本文下載。

  應(yīng)用程序

  源代碼

如何使用

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

這里可以看到的操作:

1. 安裝指定路徑的服務(wù),

2. 運(yùn)行指定服務(wù),

3. 停止正在運(yùn)行的服務(wù),

4. 卸載服務(wù),

這些功能是怎么通過代碼來實(shí)現(xiàn)的呢,我后面再說。先對它有個(gè)印象就可以了。

代碼解析

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服務(wù)!"; 7                     return; 8                 } 9                 if (ServiceIsExisted(serviceName))10                 {11                     txtTip.Text = "要安裝的服務(wù)已經(jīng)存在!";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 = "服務(wù)安裝成功!";            
View Code

上面這段代碼中最為中要的部分是方法 GetServiceName,通過給定路徑獲取服務(wù)的名稱。下面來看看這個(gè)方法是怎么實(shí)現(xiàn)的。

 1  /// <summary> 2         /// 獲取Windows服務(wù)的名稱 3         /// </summary> 4         /// <param name="serviceFileName">文件路徑</param> 5         /// <returns>服務(wù)名稱</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這個(gè)類的類,原因在于Windows服務(wù)都需要添加一個(gè)安裝程序,而安裝程序是繼承這個(gè)類的,

安裝以后的服務(wù)名稱是通過這個(gè)類ServiceInstaller的變量指定的,比如ServiceInstaller.ServiceName = "xxx";

3.獲取第二步Installer類里面的ServiceInstaller變量的值,然后獲取這個(gè)值的ServiceName屬性就是服務(wù)的名稱。

2.運(yùn)行功能:

 1 try 2             { 3                 string serviceName = GetServiceName(txtPath.Text.Trim()); 4                 if (string.IsNullOrEmpty(serviceName)) 5                 { 6                     txtTip.Text = "指定文件不是Windows服務(wù)!"; 7                     return; 8                 } 9                 if (!ServiceIsExisted(serviceName))10                 {11                     txtTip.Text = "要運(yùn)行的服務(wù)不存在!";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 = "服務(wù)運(yùn)行成功!";19                 }20                 else21                 {22                     txtTip.Text = "服務(wù)正在運(yùn)行!";23                 }24             }25             catch (Exception ex)26             {27                 txtTip.Text = ex.InnerException.ToString();28             }
View Code

重要的是ServiceController這個(gè)類,這個(gè)類可以獲取系統(tǒng)中所有的服務(wù)

 1         /// <summary> 2         /// 判斷服務(wù)是否已經(jīng)存在 3      /// </summary> 4         /// <param name="serviceName">服務(wù)名稱</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服務(wù)!"; 9                     return;10                 }11                 if (!ServiceIsExisted(serviceName))12                 {13                     txtTip.Text = "要停止的服務(wù)不存在!";14                     return;15                 }16                 ServiceController service = new ServiceController(serviceN
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 墨竹工卡县| 苍南县| 乐至县| 连州市| 华坪县| 奉化市| 鄢陵县| 澄江县| 翁牛特旗| 南充市| 丁青县| 于田县| 酉阳| 二手房| 米易县| 玉门市| 英德市| 井陉县| 惠州市| 松原市| 晋州市| 常熟市| 三原县| 崇阳县| 于田县| 苏尼特左旗| 闸北区| 西华县| 韶关市| 峡江县| 婺源县| 萝北县| 枝江市| 双城市| 潢川县| 明溪县| 恩施市| 乳山市| 揭东县| 鹿邑县| 明溪县|