提供一个操作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);
        }

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

 

 

转载于:https://www.cnblogs.com/johnwu/archive/2010/03/25/1694524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值