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

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,通过给定路径获取服务的名称。下面来看看这个方法是怎么实现的。

 

/// <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;
            }
        }
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这个类,这个类可以获取系统中所有的服务

 

/// <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;
        }
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.整体来说实现了服务的整个功能,可以方便的运行停止服务,而不再是使用命令行的方式。

 

转自 http://www.cnblogs.com/yanweidie/p/3542670.html

 

posted @ 2015-08-23 11:00  云中雀  阅读(334)  评论(0编辑  收藏  举报