首页 > 学院 > 开发设计 > 正文

使用工具安装,运行,停止,卸载Window服务

2019-11-17 03:22:05
字体:
来源:转载
供稿:网友

使用工具安装,运行,停止,卸载Window服务

WSWinForm.exe介绍

WSWinForm.exe是我自己开发的一个实用的小工具,用于将任何EXE程序作为Windows服务运行。也就是说WSWinForm只是其注册程序的服务外壳,这个特性对于我们来说非常实用,我们可以通过它来安装,运行,停止,卸载Windows服务,而不再是通过命令行InstallUtil的方式来安装。

资源下载

你可以通过本文下载。

  应用程序

  源代码

如何使用

下载完软件以后,我们能干些什么呢?看看这个截图吧:。

这里可以看到的操作:

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                 else21                 {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(serviceN
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表