• 提供一个操作Windows服务类库(基本函数)


    本类库只系对基本的Windows服务操作,没涉及到深入。我想大致的已经够用了。

    可以改造一些批量以及依赖关系。

        /// <summary>
        
    /// Windows服务类
        
    /// </summary>
        public class ServiceUtil
        {
            
    /// <summary>
            
    /// 效验服务是否存在
            
    /// </summary>
            
    /// <param name="serviceName">Windows服务名</param>
            
    /// <returns>存在返回 true,否则返回 false;</returns>
            public static bool ServiceIsExisted(string serviceName)
            {
                ServiceController[] services 
    = ServiceController.GetServices();
                
    foreach (ServiceController s in services)
                    
    if (s.ServiceName.ToLower() == serviceName.ToLower())
                        
    return true;
                
    return false;
            }

            
    /// <summary>
            
    /// 安装服务
            
    /// </summary>
            
    /// <param name="stateSaver">集合</param>
            
    /// <param name="filepath">Windows服务文件</param>
            public static void InstallService(IDictionary stateSaver, string filepath)
            {
                
    try
                {
                    AssemblyInstaller myAssemblyInstaller 
    = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext 
    = true;
                    myAssemblyInstaller.Path 
    = filepath;
                    myAssemblyInstaller.Install(stateSaver);
                    myAssemblyInstaller.Commit(stateSaver);
                    myAssemblyInstaller.Dispose();
                }
                
    catch (Exception ex)
                {
                    
    throw new SysException(ex.Message, ex);
                }
            }

            
    /// <summary>
            
    /// 卸载服务
            
    /// </summary>
            
    /// <param name="filepath">Windows服务文件</param>
            public static void UnInstallService(string filepath)
            {
                
    try
                {
                    AssemblyInstaller myAssemblyInstaller 
    = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext 
    = true;
                    myAssemblyInstaller.Path 
    = filepath;
                    myAssemblyInstaller.Uninstall(
    null);
                    myAssemblyInstaller.Dispose();
                }
                
    catch (Exception ex)
                {
                    
    throw new SysException(ex.Message, ex);
                }
            }

            
    /// <summary>
            
    /// 判断Windows服务是否正在运行
            
    /// </summary>
            
    /// <param name="name">Windows服务名</param>
            
    /// <returns>
            
    ///     <c>true</c> if the specified name is run; otherwise, <c>false</c>.
            
    /// </returns>
            public static bool IsRun(string name)
            {
                
    bool IsRun = false;
                
    try
                {
                    
    if (!ServiceIsExisted(name)) return false;
                    var sc 
    = new ServiceController(name);
                    
    if (sc.Status == ServiceControllerStatus.StartPending || sc.Status == ServiceControllerStatus.Running)
                    {
                        IsRun 
    = true;
                    }
                    sc.Close();
                }
                
    catch
                {
                    IsRun 
    = false;
                }
                
    return IsRun;
            }

            
    /// <summary>
            
    /// 启动服务
            
    /// </summary>
            
    /// <param name="name">服务名</param>
            
    /// <returns>启动成功返回 true,否则返回 false;</returns>
            public static bool StarService(string name)
            {
                
    try
                {
                    var sc 
    = new ServiceController(name);
                    
    if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending)
                    {
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running, 
    new TimeSpan(0010));
                    }
                    
    else
                    {
                        
                    }
                    sc.Close();
                    
    return true;
                }
                
    catch (Exception ex)
                {
                    
    throw new SysException(ex.Message, ex);
                }
            }

            
    /// <summary>
            
    /// 停止服务
            
    /// </summary>
            
    /// <param name="name">服务名</param>
            
    /// <returns>停止成功返回 true,否则返回 false;</returns>
            public static bool StopService(string name)
            {
                
    try
                {
                    var sc 
    = new ServiceController(name);
                    
    if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending)
                    {
                        sc.Stop();
                        sc.WaitForStatus(ServiceControllerStatus.Stopped, 
    new TimeSpan(0010));
                    }
                    
    else
                    {
                        
                    }
                    sc.Close();
                    
    return true;
                }
                
    catch (Exception ex)
                {
                    
    throw new SysException(ex.Message, ex);
                }
            }

            
    /// <summary>
            
    /// 重启服务
            
    /// </summary>
            
    /// <param name="name">服务名</param>
            
    /// <returns>重启成功返回 true,否则返回 false;</returns>
            public static bool RefreshService(string name)
            {
                
    return StopService(name) && StarService(name);
            }
        }

    作者:JohnWu
    出处:http://www.cnblogs.com/johnwu/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接
    如有问题,可以通过v.la@Live.cn 联系我,非常感谢。

  • 相关阅读:
    学习笔记-记ActiveMQ学习摘录与心得(一)
    c#写个基础的Socket通讯
    c#配置文件appStrings配置节的读取、添加和修改
    做个无边框winform窗体,并美化界面
    winform模拟鼠标按键
    winform程序开机自动启动代码
    记入园第一天
    HTTP Post 测试工具 (C#源代码)
    VC下调试内存泄漏的办法
    VMware共享文件夹遇到的问题
  • 原文地址:https://www.cnblogs.com/johnwu/p/1694524.html
Copyright © 2020-2023  润新知