zoukankan      html  css  js  c++  java
  • 提供一个操作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 联系我,非常感谢。

  • 相关阅读:
    Ubutun 安装oh-my-zsh
    归并算法
    3n+1问题
    插入排序
    二分查找
    rehl7配置本地yum仓库
    解决Anaconda3报错:AttributeError: '_NamespacePath' object has no attribute 'sort'
    深度学习情感分析(随机梯度下降代码实现)
    MySQL中创建数据库/表(外键约束),向表中插入数据
    python---利用unittestreport模块输出测试报告
  • 原文地址:https://www.cnblogs.com/johnwu/p/1694524.html
Copyright © 2011-2022 走看看