关于服务Service的操作

1.在使用ServiceController类前,我们必须引用命名空间System.ServiceProcess,而引用该命名空间,我们又必须在引用中添加System.ServiceProcess引用
2.ServiceController有两个名称,一个是DisplayName,一个是ServiceName,DisplayName-服务的友好名称,可用于标识该服务(就是我们在"控制面板"- >"服务"中看到的显示名称)ServiceName--服务进行标识的名称(这个才是服务的真正名称,也就是我们在"控制面板"- >"服务"中看到的服务名称)
3.ServiceController中其它一些有用的方法与属性:

Status属性:指示该服务现在是正在运行、已停止还是已暂停,或者启动、停止、暂停或继续命令被挂起
Pause()方法:暂停该服务。
Start()方法:启动该服务。
Stop()方法:停止该服务。

#region 安装服务  
/// <summary>
/// 安装服务  
/// </summary>  
private bool InstallService(string NameService)  
{  
    bool flag = true;  
    if (!IsServiceIsExisted(NameService))  
    {  
        try  
        {  
            string location = System.Reflection.Assembly.GetExecutingAssembly().Location;  
            string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe";  
            InstallmyService(null, serviceFileName);  
        }  
        catch   
        {  
            flag = false;  
        }  

    }  
    return flag;  
}  
#endregion  
#region 检查服务存在的存在性  
/// <summary>  
/// 检查服务存在的存在性  
/// </summary>  
/// <param name=" NameService ">服务名</param>  
/// <returns>存在返回 true,否则返回 false;</returns>  
public static bool IsServiceIsExisted(string NameService)  
{  
    ServiceController[] services = ServiceController.GetServices();  
    foreach (ServiceController s in services)  
    {  
        if (s.ServiceName.ToLower() == NameService.ToLower())  
        {  
            return true;  
        }  
    }  
    return false;  
}  
#endregion  
#region 安装Windows服务  
/// <summary>  
/// 安装Windows服务  
/// </summary>  
/// <param name="stateSaver">集合</param>  
/// <param name="filepath">程序文件路径</param>  
public static void InstallmyService(IDictionary stateSaver, string filepath)  
{  
    AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();  
    AssemblyInstaller1.UseNewContext = true;  
    AssemblyInstaller1.Path = filepath;  
    AssemblyInstaller1.Install(stateSaver);  
    AssemblyInstaller1.Commit(stateSaver);  
    AssemblyInstaller1.Dispose();  
}  
#endregion  
#region 卸载Windows服务  
/// <summary>  
/// 卸载Windows服务  
/// </summary>  
/// <param name="filepath">程序文件路径</param>  
public static void UnInstallmyService(string filepath)  
{  
    AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();  
    AssemblyInstaller1.UseNewContext = true;  
    AssemblyInstaller1.Path = filepath;  
    AssemblyInstaller1.Uninstall(null);  
    AssemblyInstaller1.Dispose();  
}  
#endregion  
#region 判断window服务是否启动  
/// <summary>  
/// 判断某个Windows服务是否启动  
/// </summary>  
/// <returns></returns>  
public static bool IsServiceStart(string serviceName)  
{  
    ServiceController psc = new ServiceController(serviceName);  
    bool bStartStatus = false;  
    try  
    {  
        if (!psc.Status.Equals(ServiceControllerStatus.Stopped))  
        {  
            bStartStatus = true;  
        }  

        return bStartStatus;  
    }  
    catch (Exception ex)  
    {  
        throw new Exception(ex.Message);  
    }  
}  
#endregion  
#region  修改服务的启动项  
/// <summary>    
/// 修改服务的启动项 2为自动,3为手动    
/// </summary>    
/// <param name="startType"></param>    
/// <param name="serviceName"></param>    
/// <returns></returns>    
public static bool ChangeServiceStartType(int startType, string serviceName)  
{  
    try  
    {  
        RegistryKey regist = Registry.LocalMachine;  
        RegistryKey sysReg = regist.OpenSubKey("SYSTEM");  
        RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");  
        RegistryKey services = currentControlSet.OpenSubKey("Services");  
        RegistryKey servicesName = services.OpenSubKey(serviceName, true);  
        servicesName.SetValue("Start", startType);  
    }  
    catch (Exception ex)  
    {  

        return false;  
    }  
    return true;  


}  
#endregion  
#region 启动服务  
private bool StartService(string serviceName)  
{  
    bool flag = true;  
    if (IsServiceIsExisted(serviceName))  
    {  
        System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);  
        if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)  
        {  
            service.Start();  
            for (int i = 0; i < 60; i++)  
            {  
                service.Refresh();  
                System.Threading.Thread.Sleep(1000);  
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)  
                {  
                    break;  
                }  
                if (i == 59)  
                {  
                    flag = false;  
                }  
            }  
        }  
    }  
    return flag;  
}  
#endregion  
#region 停止服务  
private bool StopService(string serviceName)  
{  
    bool flag = true;  
    if (IsServiceIsExisted(serviceName))  
    {  
        System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);  
        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)  
        {  
            service.Stop();  
            for (int i = 0; i < 60; i++)  
            {  
                service.Refresh();  
                System.Threading.Thread.Sleep(1000);  
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)  
                {  
                    break;  
                }  
                if (i == 59)  
                {  
                    flag = false;  
                }  
            }  
        }  
    }  
    return flag;  
}  
#endregion  

版权声明:
作者:亦灵一梦
链接:https://blog.haokaikai.cn/2021/program/aspnet/1106.html
来源:开心博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
关于服务Service的操作
1.在使用ServiceController类前,我们必须引用命名空间System.ServiceProcess,而引用该命名空间,我们又必须在引用中添加System.ServiceProcess引用 2.Service……
<<上一篇
下一篇>>