/// <summary>

/// 判断是否安装了某个服务

/// </summary>

/// <param name="serviceName"></param>

/// <returns></returns>

public static bool ISWindowsServiceInstalled(string serviceName)

{

try

{

ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)

{

if (service.ServiceName == serviceName)

{

return true;

}

}
                return false;

}

catch

{ return false; }

}

   /// <summary>
/// 启动某个服务
/// </summary>
/// <param name="serviceName"></param>
public static void StartService(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
}
}
catch { }
}
/// <summary>
/// 停止某个服务
/// </summary>
/// <param name="serviceName"></param>
public static void StopService(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
service.Stop(); service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
}
}
catch { }
}
       /// <summary>
/// 判断某个服务是否启动
/// </summary>
/// <param name="serviceName"></param>
public static bool ISStart(string serviceName)
{
bool result = true; try
{
ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
if ((service.Status == ServiceControllerStatus.Stopped)
|| (service.Status == ServiceControllerStatus.StopPending))
{
result = false;
}
}
}
}
catch { } return result;
}

关于windows服务的操作的更多相关文章

  1. windows服务管理操作

    服务程序是windows上重要的一类程序,它们虽然不与用户进行界面交互,但是它们对于系统有着重要的意义.windows上为了管理服务程序提供了一个特别的程序:服务控制管理程序,系统上关于服务控制管理的 ...

  2. C#对Windows服务的操作

    一.安装服务: private void InstallService(IDictionary stateSaver, string filepath) { try { System.ServiceP ...

  3. C#开发可以可视化操作的windows服务

    使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一 直执行我们开发的某些程序的时候.我经常看到许多人开发的win ...

  4. 【C#】开发可以可视化操作的windows服务

    使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一直执行我们开发的某些程序的时候.这里我以一个WCF的监听服务为 ...

  5. C#开发人员能够可视化操作windows服务

    使用C#开发自己的定义windows服务是一个很简单的事.因此,当.我们需要发展自己windows它的服务.这是当我们需要有定期的计算机或运行某些程序的时候,我们开发.在这里,我有WCF监听案例,因为 ...

  6. C#开发Windows服务的基础代码

    做项目需要对Windows服务进行操作,从网上找了一些资料,总结如下: (以下程序在程序中测试通过) using System; using System.Collections.Generic; u ...

  7. 网页启动Windows服务

    如何在网页启动Windows服务   由于公司有许多windows服务进行业务的处理,所谓对服务的维护也是一个比较头痛的问题,因为自己也不知道服务什么时候自动停了,而且更主要的原因是服务都是由运维部门 ...

  8. 如何在网页启动Windows服务

    由于公司有许多windows服务进行业务的处理,所以对服务的维护也是一个比较头痛的问题,因为自己也不知道服务什么时候自动停了,而且更主要的原因是服务都是由运维部门在维护管理,开发这边没有直接操作服务的 ...

  9. C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)

    译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)),不对的地方欢迎指出与交流. 章节出自<Professional C ...

随机推荐

  1. The number of positions

    Description The number of positions time limit per test: 0.5 second memory limit per test: 256 megab ...

  2. sim卡中短信简要格式

    //SELECT A0 A4 00 00 02 3F 00 9F 17 //A0 A4 00 00 02 是命令头,CLA = A0表示GSM应用,INS = A4 表示SELECT,P1 P2 =  ...

  3. mini KMS Activator v1.3破解激活microsoft Office 2010 RTM

    利用mini KMS Activator v1.3破解激活microsoft Office 2010 RTM方法,只是为体验office而做测试使用的哦...大家觉得好就自觉购买正版去... 使用步骤 ...

  4. C++编程学习52个经典网站 强力推荐

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码.这份清单提供了源代码的链接以及它们的小说明.我已尽力包括最佳的C/C++源代码的网站.这不是一个完整的清 ...

  5. SDL介绍

    SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开发者只要用相同或是相似的代码就可以开发 ...

  6. Ubuntu下获取Nexus7的Root权限

    一.准备 下载获取Root权限的工具包. 下载地址: http://downloadandroidrom.com/file/Nexus7/rooting/Nexus7Root.zip 二.解锁 Ubu ...

  7. python <tab>自动补全

    1.获取python目录[我使用的是64位ubuntu系统] [~$]python Python 2.7.3 (default, Apr 10 2013, 06:20:15) [GCC 4.6.3] ...

  8. Eclipse debug ‘Source not found’

    用Eclispe进行Debug时一直被一个问题所困扰:Source not found. 问题产生的原因是调试进入了一个没有源代码的jar包里. 简短说明: Edit Source Lookup Pa ...

  9. pywebkitgtk安装出现的问题

    configure 文件里 print sys.prefix 等不能支持python3的原因 依据http://blog.csdn.net/jklfjsdj79hiofo/article/detail ...

  10. ThreadPool.QueueUserWorkItem的性能问题

    在WEB开发中,为了降低页面等待时间提高用户体验,我们往往会把一些浪费时间的操作放到新线程中在后台执行. 简单的实现代码就是: //代码一 new Thread(()=>{ //do somet ...