安装服务和卸载服务采用process启动命令行的方式。启动服务和停止服务用到了 System.ServiceProcess.ServiceController这个类。
 
程序是这样运行的,首先启动服务和停止服务会去判断服务是否存在,根据的是服务名称,而这个名称我卸载App.config文件下,通过appsettings加key和value,程序在load事件里读取这个名称,如果不存在,就不执行相应功能。
 
当这个服务存在了之后,在去判断服务所处的状态,
if (star_service.Status != ServiceControllerStatus.Running &&
star_service.Status != ServiceControllerStatus.StartPending)
满足这个条件,才能启动服务。
 
停止服务满足这样的条件:
if (star_service.Status == ServiceControllerStatus.Running),就会执行停止服务的代码。
 
安装与卸载,是通过这个工具:InstallUtil.exe,这个工具需要根据服务所对应的.net framework版本来确定 。
 
如果是3.5及以下的版本要使用这个文件夹下的:C:\Windows\Microsoft.NET\Framework\v2.0.50727
 
如果是4.0及以上的版本写的服务要用这个文件夹下的:C:\Windows\Microsoft.NET\Framework\v4.0.30319
 
下面给出封装的命令行,只需传个服务所在路径就可以了。
 private string excuteCmd(string strCMD)
{ Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
//这里根据需要安装服务的框架版本不同,路径也不同,我的是.net3.5的
p.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727"; //p.StartInfo.CreateNoWindow = true;
//p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();//启动程序 p.StandardInput.WriteLine(strCMD);
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine(" exit");
p.StandardInput.AutoFlush = true; //获取cmd窗口的输出信息
string output = p.StandardOutput.ReadToEnd();
//等待程序执行完退出进程
p.WaitForExit();
p.Close(); return output;
}
在给个启动服务的代码:

private string excuteCmd(string strCMD)
{ Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
//这里根据需要安装服务的框架版本不同,路径也不同,我的是.net3.5的
p.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727"; //p.StartInfo.CreateNoWindow = true;
//p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();//启动程序 p.StandardInput.WriteLine(strCMD);
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine(" exit");
p.StandardInput.AutoFlush = true; //获取cmd窗口的输出信息
string output = p.StandardOutput.ReadToEnd();
//等待程序执行完退出进程
p.WaitForExit();
p.Close(); return output;
} 在给个启动服务的代码: private void btn_startService_Click(object sender, EventArgs e)
{
if (!IsServerExists(serviceName))
{
MessageBox.Show("未找到配置文件写的服务:" + serviceName);
return;
}
bool isStarted = false;
ServiceController star_service = new ServiceController(serviceName);
if (star_service.Status != ServiceControllerStatus.Running &&
star_service.Status != ServiceControllerStatus.StartPending)
{
star_service.Start();
//这里是看80s内,服务是否启动完毕
for (int i = 0; i < 80; i++)
{
star_service.Refresh();
System.Threading.Thread.Sleep(1000);
if (star_service.Status == ServiceControllerStatus.Running)
{
isStarted = true;
break;
}
if (i == 79)
{
isStarted = false;
}
}
}
else
{
MessageBox.Show("服务已经启动或被暂停");
return;
}
if (isStarted)
{
MessageBox.Show("服务启动完成!");
}
else
{
MessageBox.Show("服务未在80秒内启动起来,请到服务里去手动启动");
} }
卸载服务:
string strCMD = @"InstallUtil.exe /u " + txt_filePath.Text;
string output = excuteCmd(strCMD);
安装服务:

string strCMD = @"InstallUtil.exe " + txt_filePath.Text;

string output = excuteCmd(strCMD);

代码下载

InstallService.zip

winform——windows 服务的安装 卸载 启动 停止的更多相关文章

  1. 通过批处理进行Windows服务的安装/卸载&启动/停止

    安装服务 @echo off set checked=2 set PATHS=%~sdp0 echo 按任意键执行安装……? pause>nul if %checked% EQU 2 ( %PA ...

  2. Windows服务的安装卸载及错误查找

    @echo off echo 清理原有服务项. . . %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil /U D:\abc\te ...

  3. windows 服务的安装、启动、状态查询、停止操作c++实现

    具体的自己看看代码 粘贴复制即可使用 卸载也很简单自己查看MSDN 加上就是 #ifndef __SERVICEMANAGE_H__ #define __SERVICEMANAGE_H__ #incl ...

  4. windows服务,安装、启动、停止,配置,一个批处理文件搞定

    相对而言,还是比较通用的吧,如果哪位仁兄有更好的实现方式,或者发现有不足之处,还请多多指教.  @echo off echo.------------------------------------- ...

  5. WCF 下的windows服务的安装卸载

    安装:启动vs2010(如果是win2008要以管理员来启动)命令:installutil demo.exe 卸载:先在服务里停止这个服务,然后启动vs2010(如果是win2008要以管理员来启动) ...

  6. C# Windows服务创建安装卸载

    一.创建Windows服务 使用VS创建一个新的windows服务应用程序 创建完成之后 二.相关配置 修改Service1名称为StartService(可以不改,自行选择) 添加安装程序并修改配置 ...

  7. C#操作windows服务,安装、卸载、停止、启动

    public class ServiceUtil { private string _ServiceName = string.Empty; private string _AppName = str ...

  8. windows操作系统中安装、启动和卸载memcached

    今天总结一下如何在Windows操作系统中安装.启动和卸载memcached:下载地址: http://download.csdn.net/download/wangshuxuncom/8249501 ...

  9. 使用InstallUtil对Windows服务进行安装与卸载

    关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...

  10. Windows Service的安装卸载 和 Service控制(转)

    Windows Service的安装卸载 和 Service控制 原文地址:http://www.cnblogs.com/Peter-Zhang/archive/2011/10/15/2212663. ...

随机推荐

  1. Java 内存分析(程序实例),学会分析内存,走遍天下都不怕!!!

    相信大多数的java初学者都会有这种经历:碰到一段代码的时候,不知该从何下手分析,不知道这段代码到底是怎么运行最后得到结果的..... 等等吧,很多让人头疼的问题,作为一名合格的程序员呢,遇到问题一定 ...

  2. Windows wsl2安装Ubuntu

    wsl(Windows Subsystem for Linux)即适用于Windows的Linux子系统,是一个实现在Windows 10 / 11上运行原生Linux的技术. wsl2 为其迭代版本 ...

  3. openGauss中如何管理表空间

    openGauss 中如何管理表空间 在 openGauss 中,表空间是一个目录,在物理数据和逻辑数据间提供了抽象的一层,为所有的数据库对象分配存储空间,里面存储的是它所包含的数据库的各种物理文件. ...

  4. Mybatis实现增删改查

    ​1.CRUD 1.1namespace namespace中的包名必须和Dao/mapper接口包名一致 1.2select 选择,查询语句 id:就是对应的namespace中的方法名 resul ...

  5. git worktree与分支依赖隔离

    git worktree介绍 git worktree 是 Git 命令,用于管理多分支工作区. 使用场景: 同时维护不同分支,隔离分支依赖差异:从原有项目开辟一个分支作为另一个新项目,当两个项目依赖 ...

  6. HarmonyOS应用性能与功耗云测试

    性能测试 性能测试主要验证HarmonyOS应用在华为真机设备上运行的性能问题,包括启动时长.界面显示.CPU占用和内存占用.具体性能测试项的详细说明请参考性能测试标准. 性能测试支持Phone和TV ...

  7. cesiumjs GIS引擎源码编译并运行-2021年3月18日最新版【1.68~1.79.1版本亲测成功】

    前言 本篇最初是在2020年的[macOS Big Sur + Cesium 1.76版本]下编译成功,后在[macOS Catalina+cesium 1.79.1版本]编译过程中,出现编译的错误和 ...

  8. mysql 必知必会整理—安全管理[十七]

    前言 简单介绍一下安全管理. 正文 MySQL服务器的安全基础是:用户应该对他们需要的数据具有适当的访问权,既不能多也不能少. 换句话说,用户不能对过多的数据具有过多的访问权. 多数用户只需要对表进行 ...

  9. EVA: Visual Representation Fantasies from BAAI

    ​本文做个简单总结,博主不是做自监督领域的,如果错误,欢迎指正. 链接 Code:​ Official:baaivision/EVA MMpretrain:open-mmlab/mmpretrain/ ...

  10. Jenkins实战系列(一)——Jenkins简介

    Jenkins是一个开源的自动化构建工具,可以帮助开发人员自动构建.测试和部署软件.它支持多种编程语言.版本控制系统和构建工具,如Java.Git.Maven等.Jenkins的核心功能是通过一系列插 ...