使用winform程序控制window服务的操作
继上篇
c#之添加window服务(定时任务)
基础之上,
这篇文章主要讲述,使用winform程序来控制window服务的安装,启动,停止,卸载等操作
1.在同一个解决方案添加winform项目,如图
2.在winform设计器中添加按钮
修改按钮名称依次为:安装服务、启动服务、停止服务、卸载服务
3.向winform项目中添加引用:System.Configuration.Install 和 System.ServiceProcess 两个程序集(其中涉及window服务的一些操作)
4.把之前创建的window服务也引入winform中
5.编写代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //安装及卸载路径
string serviceFilePath = $"{Application.StartupPath}\\WindowService_HelloWorld.exe";
//服务名称
string serviceName = "MyService"; /// <summary>
/// 事件:安装服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (IsServiceExisted(serviceName)) this.UninstallService(serviceName);
this.InstallService(serviceFilePath);
} /// <summary>
/// 事件:启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
} /// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
} /// <summary>
/// 卸载服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
} /// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach(ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
} /// <summary>
/// 安装服务
/// </summary>
/// <param name="serviceFilePath"></param>
private void InstallService(string serviceFilePath)
{
using(AssemblyInstaller installer=new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary saveState = new Hashtable();
installer.Install(saveState);
installer.Commit(saveState);
}
} /// <summary>
/// 卸载服务
/// </summary>
/// <param name="serviceFilePath"></param>
private void UninstallService(string serviceFilePath)
{
using(AssemblyInstaller installer=new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
}
} /// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName"></param>
private void ServiceStart(string serviceName)
{
using(ServiceController controller=new ServiceController(serviceName))
{
if (controller.Status == ServiceControllerStatus.Stopped)
{
controller.Start();
}
}
} /// <summary>
/// 停止服务
/// </summary>
/// <param name="serviceName"></param>
private void ServiceStop(string serviceName)
{
using(ServiceController controller=new ServiceController(serviceName))
{
if (controller.Status == ServiceControllerStatus.Running)
{
controller.Stop();
}
}
} }
结构如下:
6.在项目winform中,右击项目,添加->新建项,在弹出的菜单中选择 应用程序清单文件,如图
7.打开该文件,并将<requestedExecutionLevel level="asInvoker" uiAccess="false" />改为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,如下图所示:
8.重新生成解决方案,并运行(winform自己不知道自己是谁)
点击安装,打开window服务管理器,发现MyService安装上了,后续几个按钮依次实验,正常
另外还有一些调试技巧,这里就不多说了
给出参考网址:
https://www.cnblogs.com/cncc/p/7170951.html
https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer
最新文章
- Oracle存储过程语法
- asp.net js 跨域方法二
- T-SQL 小数点转换百分数
- [php] 使用IDE的正则搜索代码
- 基于C#实现的HOOK键盘钩子实例代码
- 运行edX Devstack
- oschina 开发工具
- MySQL的常见存储引擎介绍与参数设置调优
- 将CSV文件存为HTML文件形式
- 英语口语练习系列-C08-考试
- react-native---rn中的修饰组件(TouchableHightlight、TouchableOpacity、TouchableNativeFeedback等)
- tiny6410采用sd卡烧写的问题
- stm32 硬件错误
- LeetCode--013--罗马数字转整数(java)
- MVC无刷新上传图片并显示
- SDN 第四次上机作业
- 扩展欧几里得(E - The Balance POJ - 2142 )
- Tomcat下设置项目为默认项目
- spring boot 学习(三)API注解记录及测试
- 多线程同步与并发访问共享资源工具—Lock、Monitor、Mutex、Semaphore
热门文章
- mybatis 获取新增数据的主键
- mysql 获取单个科目的平均分
- 使用python3完成人脸识别
- 检测算法简介及其原理——fast R-CNN,faster R-CNN,YOLO,SSD,YOLOv2,YOLOv3
- ArgumentException: The Assembly Mono.WebBrowser is referenced by System.Windows.Forms (&#39;Assets/Plugins/System.Windows.Forms.dll&#39;). But the dll is not allowed to be included or could not be found.
- 上传图片 展示进度条 bootstrap
- gradle 使用maven repository 的设置
- android: android 布局中的weight 属性
- Oracle系列三 过滤和排序
- Eclipse新项目检出后报错第一步:导入lib中的jar包【我】