启动服务的方法有很多种,简单的cmd下dos命名,手动启动,还有C#代码启动。

我们要实现的功能:

  • 判断是否安装
  • 是否启动
  • 启动服务
  • 关闭服务

我封装了有关服务的代码,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace windows服务
{
public static class ServicesHelper
{
/// <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(, , ));
}
}
}
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(, , ));
}
}
}
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;
}
}
}

C#启动服务的更多相关文章

  1. git克隆项目到本地&&全局安装依赖项目&&安装依赖包&&启动服务

     一.安装本地开发环境 1.安装本项目 在需要保存到本地的项目的文件夹,进入到文件夹里点击右键,bash here,出现下图: 2.安装依赖项目  3.安装依赖包(进入到命令行) # 安装依赖包 $ ...

  2. MySQL 启动服务报错解决方案

    标签:ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid) 概述 文章 ...

  3. ubuntu下设置开机启动服务

    原文:http://blog.csdn.net/dante_k7/article/details/7213151 在ubuntu10.04之前的版本都是使用chkconfig来进行管理,而在之后的版本 ...

  4. C#操作注册服务卸载服务启动服务停止服务.. .

    using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; usi ...

  5. 如何添加Tomcat为启动服务

    tomcat8.0, 可以用startup.bat启动, 但注销又不能启动服务,但现在的系统不知出了什么问题 ? 答 1.我这个是zip版本的,所以里面有一个service.bat的文件,所以很简单 ...

  6. 【转】Win8下安装SQL Server 2005无法启动服务

    安装了Windows8,但是发现不支持Sql Server 2005的安装.网上找了很多办法,基本上都有缺陷.现在终于找到一种完全正常没有缺陷的办法了,和大家分享一下. 1.正常安装任一版本的SQL ...

  7. centos 开机启动服务

    一.启动脚本 /etc/rc.local 启动 最简单的一种方式,在启动脚本 /etc/rc.local (其实 /etc/rc.local 是/etc/rc.d/rc.local 的软链接文件,实际 ...

  8. MongoDB安装启动服务

    这里主要是针对解决出现的问题. 1.MongoDB 下载地址http://www.mongodb.org/downloads 下载之后可以自定义安装路径,我安装在了D盘下. 然后在环境变量中添加: M ...

  9. MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”

    MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...

  10. 【SAP BO】【DI】DataService 服务无法启动。错误1069:由于登录失败而无法启动服务

    重启BI服务器后,突然发现DataServices服务无法启动,提示: 错误 1069:由于登录失败而无法启动服务. 解决方法: (1)打开控制面板--> 服务 --> 右键点击Data ...

随机推荐

  1. 在Vue项目中使用html2canvas生成页面截图并上传

    使用方法 项目中引入 npm install html2canvas html代码 //html代码 <!-- 把需要生成截图的元素放在一个元素容器里,设置一个ref --> <di ...

  2. python - 斐波那契(Fibonacci)数列

    斐波那契数列即数列中每一项等于它前面两项的和,公式如下: f(n) = f(n-1) + f(n-2)    n>2        -----        递推公式 f(n) = 1     ...

  3. XML 十六进制值 是无效的字符错误 解决方法之一 转

    /// <summary> /// 过滤非打印字符 /// </summary> /// <param name="tmp">待过滤</p ...

  4. selenium+Python(alert 、confirm 、prompt 的处理)

    alert\confirm\prompt 弹出框操作主要方法有: text  返回 alert/confirm/prompt 中的文字信息 accept  点击确认按钮 dismiss 点击取消按钮, ...

  5. Yii “CDbConnection failed to open the DB connection: could not find driver"解决办法

    前言:用Yii框架做项目时,有时会遇到“CDbConnection failed to open the DB connection: could not find driver”这个问题,这个问题通 ...

  6. org.dbunit.dataset.NoSuchTableException: t_group

    遇到这个错误发现异常信息里有一项是这个 15:30:10,609 ERROR SchemaUpdate:236 - HHH000388: Unsuccessful: create table t_gr ...

  7. Struts2入门介绍(二)

    一.Struts执行过程的分析. 当我们在浏览器中输入了网址http://127.0.0.1:8080/Struts2_01/hello.action的时候,Struts2做了如下过程: 1.Stru ...

  8. vue中添加favicon.ico

    1.首先将favicon.ico图片放在根目录下 2.修改webpack配置文件 1)找到build下的webpack.dev.conf.js文件 new HtmlWebpackPlugin({ fi ...

  9. C++(笔)002

    #include <iostream> //预处理器编译指令 int main() //函数头:对函数和程序其它部份之间的接口作出总结 int:函数的返回类型 { using namesp ...

  10. ASP.NET MVC4 新手入门教程之九 ---9.查询详情和删除方法

    在本教程的这一部分,您会检查自动生成的Details和Delete方法. 检查详细信息和删除方法 打开Movie控制器并检查的Details的方法. public ActionResult Detai ...