博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用工具安装,运行,停止,卸载Window服务
阅读量:7085 次
发布时间:2019-06-28

本文共 6482 字,大约阅读时间需要 21 分钟。

WSWinForm.exe介绍

 

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

资源下载

 你可以通过本文下载。

 

 

如何使用

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

 

这里可以看到的操作:

1. 安装指定路径的服务,

2. 运行指定服务,

3. 停止正在运行的服务,

4. 卸载服务,

这些功能是怎么通过代码来实现的呢,我后面再说。先对它有个印象就可以了。

代码解析

 

1.安装功能:

string[] cmdline = { };                string serviceFileName = txtPath.Text.Trim();                string serviceName = GetServiceName(serviceFileName);                if (string.IsNullOrEmpty(serviceName))                {                    txtTip.Text = "指定文件不是Windows服务!";                    return;                }                if (ServiceIsExisted(serviceName))                {                    txtTip.Text = "要安装的服务已经存在!";                    return;                }                TransactedInstaller transactedInstaller = new TransactedInstaller();                AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);                transactedInstaller.Installers.Add(assemblyInstaller);                transactedInstaller.Install(new System.Collections.Hashtable());                txtTip.Text = "服务安装成功!";
View Code

上面这段代码中最为中要的部分是方法 GetServiceName,通过给定路径获取服务的名称。下面来看看这个方法是怎么实现的。

 

///         /// 获取Windows服务的名称        ///         /// 文件路径        /// 
服务名称
private string GetServiceName(string serviceFileName) { try { Assembly assembly = Assembly.LoadFrom(serviceFileName); Type[] types = assembly.GetTypes(); foreach (Type myType in types) { if (myType.IsClass && myType.BaseType == typeof(System.Configuration.Install.Installer)) { FieldInfo[] fieldInfos = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default | BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo myFieldInfo in fieldInfos) { if (myFieldInfo.FieldType == typeof(System.ServiceProcess.ServiceInstaller)) { ServiceInstaller serviceInstaller = (ServiceInstaller)myFieldInfo.GetValue(Activator.CreateInstance(myType)); return serviceInstaller.ServiceName; } } } } return ""; } catch (Exception ex) { throw ex; } }
View Code

 

1.加载程序集

 2.获取程序集里面继承于System.Configuration.Install.Installer这个类的类,原因在于Windows服务都需要添加一个安装程序,而安装程序是继承这个类的

 安装以后的服务名称是通过这个类ServiceInstaller的变量指定的,比如ServiceInstaller.ServiceName = "xxx";

 3.获取第二步Installer类里面的ServiceInstaller变量的值,然后获取这个值的ServiceName属性就是服务的名称。

 

 2.运行功能:

 

try            {                string serviceName = GetServiceName(txtPath.Text.Trim());                if (string.IsNullOrEmpty(serviceName))                {                    txtTip.Text = "指定文件不是Windows服务!";                    return;                }                if (!ServiceIsExisted(serviceName))                {                    txtTip.Text = "要运行的服务不存在!";                    return;                }                ServiceController service = new ServiceController(serviceName);                if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)                {                    service.Start();                    txtTip.Text = "服务运行成功!";                }                else                {                    txtTip.Text = "服务正在运行!";                }            }            catch (Exception ex)            {                txtTip.Text = ex.InnerException.ToString();            }
View Code

 

重要的是ServiceController这个类,这个类可以获取系统中所有的服务

 

///         /// 判断服务是否已经存在     ///         /// 服务名称        /// 
bool
private bool ServiceIsExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; }
View Code

 

 3.停止功能:

 

ry            {                string[] cmdline = { };                string serviceFileName = txtPath.Text.Trim();                string serviceName = GetServiceName(serviceFileName);                if (string.IsNullOrEmpty(serviceName))                {                    txtTip.Text = "指定文件不是Windows服务!";                    return;                }                if (!ServiceIsExisted(serviceName))                {                    txtTip.Text = "要停止的服务不存在!";                    return;                }                ServiceController service = new ServiceController(serviceName);                if (service.Status == ServiceControllerStatus.Running)                {                    service.Stop();                    txtTip.Text = "服务停止成功!";                }                else                {                    txtTip.Text = "服务已经停止!";                }            }            catch (Exception ex)            {                txtTip.Text = ex.InnerException.ToString();            }
View Code

 

4.卸载功能:

 

try            {                string[] cmdline = { };                string serviceFileName = txtPath.Text.Trim();                string serviceName = GetServiceName(serviceFileName);                if (string.IsNullOrEmpty(serviceName))                {                    txtTip.Text = "指定文件不是Windows服务!";                    return;                }                if (!ServiceIsExisted(serviceName))                {                    txtTip.Text = "要卸载的服务不存在!";                    return;                }                TransactedInstaller transactedInstaller = new TransactedInstaller();                AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);                transactedInstaller.Installers.Add(assemblyInstaller);                transactedInstaller.Uninstall(null);                txtTip.Text = "服务卸载成功!";            }            catch (Exception ex)            {                txtTip.Text = ex.InnerException.ToString();            }
View Code

 

总结

 

1.整体来说实现了服务的整个功能,可以方便的运行停止服务,而不再是使用命令行的方式。

 

转自

 

转载于:https://www.cnblogs.com/blosaa/p/4752070.html

你可能感兴趣的文章
mkdir failed for img Read-only file system
查看>>
Android中使用第三方jar包
查看>>
[转]Console命令详解,让调试js代码变得更简单
查看>>
堆是堆,栈归栈
查看>>
语法面试等题目汇总
查看>>
你写的Try...Catch真的有必要么?
查看>>
4安德鲁斯.2.2在系统,具有系统权限的应用程序无法读取或写入SD卡
查看>>
Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor...
查看>>
Android开发:使用ViewDragHelper实现抽屉拉伸效果
查看>>
CSS的position设置
查看>>
mysql实战优化之五: 更新/插入优化 sql优化
查看>>
Uber即将进驻扬州啦,车主火热招募中!
查看>>
缓存一致性协议
查看>>
JVM Input Arguments Lookup (JMX)(转)
查看>>
我持续推动Rust语言支持Windows XP系统
查看>>
Http状态码说明
查看>>
浏览器缓存相关http头
查看>>
Autofac.Integration.Owin
查看>>
NGINX反向代理
查看>>
完整部署CentOS7.2+OpenStack+kvm 云平台环境(6)--在线调整虚拟机的大小
查看>>