C#启动服务
启动服务的方法有很多种,简单的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#启动服务的更多相关文章
- git克隆项目到本地&&全局安装依赖项目&&安装依赖包&&启动服务
一.安装本地开发环境 1.安装本项目 在需要保存到本地的项目的文件夹,进入到文件夹里点击右键,bash here,出现下图: 2.安装依赖项目 3.安装依赖包(进入到命令行) # 安装依赖包 $ ...
- MySQL 启动服务报错解决方案
标签:ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid) 概述 文章 ...
- ubuntu下设置开机启动服务
原文:http://blog.csdn.net/dante_k7/article/details/7213151 在ubuntu10.04之前的版本都是使用chkconfig来进行管理,而在之后的版本 ...
- C#操作注册服务卸载服务启动服务停止服务.. .
using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; usi ...
- 如何添加Tomcat为启动服务
tomcat8.0, 可以用startup.bat启动, 但注销又不能启动服务,但现在的系统不知出了什么问题 ? 答 1.我这个是zip版本的,所以里面有一个service.bat的文件,所以很简单 ...
- 【转】Win8下安装SQL Server 2005无法启动服务
安装了Windows8,但是发现不支持Sql Server 2005的安装.网上找了很多办法,基本上都有缺陷.现在终于找到一种完全正常没有缺陷的办法了,和大家分享一下. 1.正常安装任一版本的SQL ...
- centos 开机启动服务
一.启动脚本 /etc/rc.local 启动 最简单的一种方式,在启动脚本 /etc/rc.local (其实 /etc/rc.local 是/etc/rc.d/rc.local 的软链接文件,实际 ...
- MongoDB安装启动服务
这里主要是针对解决出现的问题. 1.MongoDB 下载地址http://www.mongodb.org/downloads 下载之后可以自定义安装路径,我安装在了D盘下. 然后在环境变量中添加: M ...
- MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”
MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...
- 【SAP BO】【DI】DataService 服务无法启动。错误1069:由于登录失败而无法启动服务
重启BI服务器后,突然发现DataServices服务无法启动,提示: 错误 1069:由于登录失败而无法启动服务. 解决方法: (1)打开控制面板--> 服务 --> 右键点击Data ...
随机推荐
- [总]Android高级进阶之路
个人Android高级进阶之路,目前按照这个目录执行,执行完毕再做扩展!!!!! 一.View的绘制 1)setContentView()的源码分析 2)SnackBar的源码分析 3)利用decor ...
- 实现Map按key或按value排序
原理思路:用List实现排序,然后将List中的值遍历存入到LinkedHashMap 实现方式: //这里将map.entrySet()转换成list List<Map.Entry<St ...
- Linux下与Windows下开发软件
Linux下开发程序可以完全发挥自己的聪明才智,因为系统内核是完全开放的.Windows下开发程序就稍微郁闷一点,不论何种语言都必须在调用系统API的基础上开发,因为系统内核是不开放的. 这两种系统正 ...
- Unity QualitySettings.shadows 阴影
QualitySettings.shadows 阴影 public static ShadowQuality shadows; Description 描述: 要使用的实时阴影类型. 这就决定了应该使 ...
- sql根据表中数量字段自动复制记录行
客户需要将表中统计好的数据还原成统计前的原始记录 例如: ID Name QTYCount100 Name1 1101 Name2 2102 Name3 3103 Name4 4104 Name5 5 ...
- Bugfree安装与使用
第一步:下载XAMPP和bugfree http://www.bugfree.org.cn/ http://www.apachefriends.org/zh_cn/xampp.html 第二步:安装 ...
- python中range()和len()函数区别
函数:len() 作用:返回字符串.列表.字典.元组等长度 语法:len(str) 参数: str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度 实例 1.计算字符 ...
- lucene关于IndexReader总结
IndexReader.使用过程中有时会出现document被删除,reader还是原来的reader没有改变,所以使用openifchanged保证, 又因为IndexReader 初始化很耗费资源 ...
- Linux学习(1)
Linux操作系统核心"Kernel",位于操作系统底层,是连接Shell.KDE.应用和硬件的接口,核心必须支持的管理事物: 1)系统调用接口(System Call Inter ...
- [android] 天气app布局练习
主要练习一下RelativeLayout和LinearLayout <RelativeLayout xmlns:android="http://schemas.android.com/ ...