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.安装功能:

                 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 = "服务安装成功!";

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

  /// <summary>
/// 获取Windows服务的名称
/// </summary>
/// <param name="serviceFileName">文件路径</param>
/// <returns>服务名称</returns>
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;
}
}

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();
}

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

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

 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();
}

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();
}

总结

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

2.下一篇将讲解,使用Windows服务实现任务处理(及定时执行某个功能)。

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

如果,想给予我更多的鼓励,求打

因为,我的写作热情也离不开您的肯定支持。

感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是焰尾迭 。

使用工具安装,运行,停止,卸载Window服务的更多相关文章

  1. 【C#】使用bat文件安装卸载Window服务

    1.安装服务 @echo off @title 安装windows服务path %SystemRoot%\Microsoft.NET\Framework\v4.0.30319echo========= ...

  2. c#用控制台程序安装启动停止卸载服务

    第一步:新建控制台项目  第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上   第四步 编写代码 双击打开 using System; using Syst ...

  3. 开发工具安装运行bug总结

    如果tomcat出现闪退 在startup.bat--编辑   在文件最后加上 pause  ,再跑一次,可以看到闪退的原因. 一般是环境变量问题,只需要打开starup.bat--编辑,最方件的最上 ...

  4. 使用InstallUtil安装及卸载Windows服务的具体操作 Visual Studio 2012版本

    关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...

  5. .NET Window服务启动又马上停止,报错IO.FileNotFoundException

    最近公司需要开发一个Window服务推送系统,读取MongoDB写入消息队列,推送到各终端平台 但是在开发完成,最后的部署阶段,选中服务右击启动 看似正常,服务显示已启动(但实质已经被终止,因为Win ...

  6. C# 编写Window服务基础(一)

    一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...

  7. window 服务(一)

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...

  8. C# 编写短信发送Window服务

    我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...

  9. 自定义Window 服务

    自定义window 服务 开发到使用的流程: 1.完成对应的代码之后(代码在底下),右键MyService.cs 添加安装程序 2.添加window服务安装程序打开Service1.cs[设计]页面, ...

随机推荐

  1. extjs combobox 事件

    change---显示的值改变事件 select---选中选项事件 expand---下拉框展开事件 collapse--下拉框折叠事件 { xtype: 'container', width: 25 ...

  2. TinyMCE添加图片 路径自动处理成相对路径

    默认情况下会自动转换你的图片路径如: 转换: /path/name.jpg 为 ../path/name.jpg 带有域名的路径也会被转换为相对路径. 需要修改一个设置convert_urls,官方文 ...

  3. hibernate(1) —— 入门

    hibernate框架主要是实现数据库与实体类间的映射,使的操作实体类相当与操作hibernate框架. 只要实体类写好配置文件配好,就能实现和数据库的映射,其中实体类对应表,类的属性对应数据库的表字 ...

  4. 网页中tab标签切换分别用jquery和javascript源码实现

    //HTML布局<ul id="tabTitle"> <li class="active">HTML5</li> <l ...

  5. Objective-C 关键字:retain, assgin, copy, readonly,atomic,nonatomic

    声明式属性的使用:声明式属性叫编译期语法 @property(retain,nonatomic)Some *s; @property(参数一,参数二)Some *s; 参数1:retain:修饰引用( ...

  6. iOS之ShareSDK实现分享、第三方登录等功能

    (1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...

  7. iOS---友盟推送遇到的坑

    生产证书无效  很有可能  是你的证书上传的不对 开发证书  对应我们测试环境的推送P12文件 生产证书   对应我们线上环境的推送P12文件 device Token  无效  有可能是你的证书不对 ...

  8. 适配ios10(iTunes找不到构建版本)

    前两天上架App遇到一个比较神奇的问题,打包好的项目使用Application Loader上传成功,但是在iTunes里面却找不到构建版本,App的活动页面也没有相应的版本. 之前了解IOS10对用 ...

  9. SE(homework3)_敏捷模型

    今天老师上课主要和我们讲解了软件开发模型类型.既然是敏捷模型,那么什么是非敏捷模型呢?了解这里点,会更清楚什么是敏捷模想.我们所知道的非敏捷模型有瀑布模型,我们知道这是早期软件开发的经典模型,流程主要 ...

  10. SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法

    操作步骤: 1. 在 Microsoft Windows 2003 或 Windows XP 桌面上,依次单击"开始"."运行",然后在"打开&quo ...