现在的位置: 首页 > 综合 > 正文

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

2013年02月18日 ⁄ 综合 ⁄ 共 2947字 ⁄ 字号 评论关闭

http://www.chenjiliang.com/Article/View.aspx?ArticleID=14417

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

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

复制  保存
/// <summary>

/// Windows服务类

/// </summary>

using System;
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);
    }
}

抱歉!评论已关闭.