用C#代码来安装、卸载、启动、关闭服务
/// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("WindowsService1");
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
}
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("MSSQLSERVER");
if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Stop();
}
}
/// <summary>
/// 安装服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (!isServiceIsExisted("Service1"))
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(0, location.LastIndexOf('//') + 1) + "WindowsService1.exe";
InstallmyService(null, serviceFileName);
}
else
{
MessageBox.Show("系统已经安装了此服务!");
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (isServiceIsExisted("Service1"))
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(0, location.LastIndexOf('//') + 1) + "WindowsService1.exe";
UnInstallmyService(serviceFileName);
}
else
{
MessageBox.Show("系统不存在此服务,不需要卸载!");
}
}

/// <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;
}
/// <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();
}
/// <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();
}
用C#代码来安装、卸载、启动、关闭服务的更多相关文章
- mariaDB 安装/卸载+启动/关闭 服务
1.设置环境变量 无论是用户环境变量还是系统环境变量 2.启动服务 进入根目录 名字根据 --install 后的 参数来决定 叫MariaDB,MySQL 都可以 mysqld.exe --inst ...
- redis安装 卸载 启动 关闭
一 redis安装 第一步:在VMware中安装CentOS(参考Linux教程中的安装虚拟机) 第二步:在Linux下安装gcc环境 [root@hadoop ~]#yum install gcc- ...
- 【运维技术】VM虚拟机上使用centos7安装docker启动gogs服务教程【含B站视频教程】
VM虚拟机上使用centos7安装docker启动gogs服务视频教程 BiliBili视频教程链接飞机票,点我 使用VMware Workstation安装Centos7 MinMal系统 第一步: ...
- c#创建windows服务(代码方式安装、启动、停止、卸载服务)
转载于:https://www.cnblogs.com/mq0036/p/7875864.html 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NE ...
- redis 安装及启动关闭
1.redis下载 方式1:直接去官网下载 https://redis.io/download 方式2:通过命令下载 wget http://download.redis.io/releases/re ...
- nginx学习与配置-安装与启动关闭管理
nginx服务器的安装 安装准备: nginx依赖于pcre库,要先安装pcre yum install pcre pcre-devel cd /usr/local/src/ wget wget ht ...
- 批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务
windows(win10)批处理脚本 打开vm虚拟机的服务,并且开启无界面虚拟机 @echo off net start "vds" net start "VMAuth ...
- 使用Windows命令行启动关闭服务(net,sc用法)
下面两个命令最好以管理员方式启动cmd窗口,否则出现权限问题. 1.net用于打开没有被禁用的服务, NET命令是功能强大的以命令行方式执行的工具. 它包含了管理网络环境.服务.用户.登陆大部分重要的 ...
- ubuntu 下安装和启动SSH 服务
安装OPENSSH 服务端 sudo apt-get install openssh-server 查看进程是否启动 ps -e | grep ssh 删除密钥文件 rm /etc/ssh/ssh_h ...
- BIEE启动关闭服务(转)
一.环境说明 版本:BIEE11g (BIEE_11.1.1.9.0) OS:CentOS 6.5 64bit (所有的linux服务器都适用) 二.BIEE启动与关闭 BIEE11g 的启动包括三个 ...
随机推荐
- Java -- JDBC 学习--调用函数&存储过程
调用函数&存储过程 /** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connec ...
- A1082. Read Number in Chinese
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...
- Java: 在dos窗口输入密码,不要把密码直接显示出来,原来可以这么简单
用下面的方法可以实现在控制台上输入密码时,密码不显示在控制台上:Console cons=System.console(); System.out.print("请输入密码:"); ...
- 关于使用IsapiRewrite重写,重定向的语句
早上起床,发现之前的IsapiRewrite写法有改进的地方, 有几个废弃的网址可以使用IsapiRewrite的重定向,转到新的网址上来. 搞了有十分钟的时间,测试老是不重定向. 因为之前已经有一个 ...
- C++11并发——多线程std::mutex (二)
https://www.cnblogs.com/haippy/p/3237213.html Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mute ...
- PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in Fa ...
- Redis 安全配置
1.禁止一些高危命令 修改 redis.conf 文件,添加 rename-command FLUSHALL "" rename-command FLUSHDB "&qu ...
- python---网络之邮件发送
在发送邮件之前需要先设置邮件开启了SMTP等功能,对于163还需要获取授权码 这里设置保存修改 这里获取授权码 完整封装发送邮件类 import smtplib from email.mime.tex ...
- Oracle记录-Linux JDK与Oracle profile环境配置
1.LINUX JDK环境配置 #set java environment export JAVA_HOME=/usr/java/jdk1.7.0_79 export JRE_HOME=/usr/ja ...
- A + B,末k位不相同
题目描述 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1. 输入描述: 测试输入包含若干测试用例,每个测试用例占一行,格式为& ...