当前位置: 首页 > news >正文

动画制作软件下载安装seo数据

动画制作软件下载安装,seo数据,网站建设的色彩搭配,wordpress的主题C#开发Windows服务守护程序背景实现关键C#编程判断指定的Windows服务是否存在C#编程获取指定Windows服务状态C#编程实现服务安装、启动、停止、卸载背景 通常为了便于对指定Windows服务进行方便的管理,我们会开发一个守护程序,或者叫做服务管理的工具&a…

C#开发Windows服务守护程序

  • 背景
  • 实现关键
  • C#编程判断指定的Windows服务是否存在
  • C#编程获取指定Windows服务状态
  • C#编程实现服务安装、启动、停止、卸载

背景

通常为了便于对指定Windows服务进行方便的管理,我们会开发一个守护程序,或者叫做服务管理的工具,基本要实现windows服务的安装、卸载、启动、停止。

实现关键

System.ServiceProcess.ServiceController			//使用此类访问已安装的Windows服务

C#编程判断指定的Windows服务是否存在

/// <summary>
/// 判断指定的服务是否已存在(安装)
/// </summary>
/// <param name="serviceName">要判断服务名称</param>
/// <returns>存在(安装)返回true,不存在(未安装)返回false</returns>
public bool IsServiceExists(string serviceName)
{bool exists = false;System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();foreach (System.ServiceProcess.ServiceController s in services){if (s.ServiceName == serviceName){exists = true;break;}}return exists;
}

C#编程获取指定Windows服务状态

/// <summary>
/// 获取windows服务的状态
/// </summary>
/// <param name="serviceName">要获取状态的服务名称</param>
/// <returns>返回服务状态</returns>
public string GetServiceStatus(string serviceName)
{string result = "服务不存在";System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();foreach (System.ServiceProcess.ServiceController s in services){if (s.ServiceName == serviceName){result = s.Status.ToString();break;}}return result;
}

C#编程实现服务安装、启动、停止、卸载

1、定义变量

private string serivceExeFullPath = @"D:\BTSService\BTSDataService.exe";        //服务程序的完整路径
private string serviceName = "BTSDataService";                                  //服务名称

2、服务安装(本Windows服务是基于Topshelf框架开发)

private void btnServiceInstall_Click(object sender, EventArgs e)
{if (!System.IO.File.Exists(serivceExeFullPath)){MessageBox.Show("服务路径不存在,请确认服务程序是否存在!");return;}if (this.IsServiceExists(this.serviceName)){MessageBox.Show("服务已安装!");return;}try{using(System.Diagnostics.Process p = new System.Diagnostics.Process()){p.StartInfo.UseShellExecute = false;p.StartInfo.CreateNoWindow = true;p.StartInfo.FileName = serivceExeFullPath;p.StartInfo.Arguments = "install";p.Start();p.Close();}System.Threading.Thread.Sleep(2000);this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);MessageBox.Show("服务安装完毕!");}catch(Exception ex){MessageBox.Show("服务安装异常:" + ex.Message);}
}

3、服务启动(本Windows服务是基于Topshelf框架开发)

private void btnServiceStart_Click(object sender, EventArgs e)
{if (!this.IsServiceExists(this.serviceName)){MessageBox.Show("服务未安装,请先安装!");return;}try{using (System.Diagnostics.Process p = new System.Diagnostics.Process()){p.StartInfo.UseShellExecute = false;p.StartInfo.CreateNoWindow = true;p.StartInfo.FileName = serivceExeFullPath;p.StartInfo.Arguments = "start";p.Start();p.Close();}System.Threading.Thread.Sleep(2000);this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);MessageBox.Show("服务启动成功!");}catch (Exception ex){MessageBox.Show("服务安装异常:" + ex.Message);}
}

4、服务停止(本Windows服务是基于Topshelf框架开发)

private void btnServiceStop_Click(object sender, EventArgs e)
{if (!this.IsServiceExists(this.serviceName)){MessageBox.Show("服务未安装,请先安装!");return;}try{using (System.Diagnostics.Process p = new System.Diagnostics.Process()){p.StartInfo.UseShellExecute = false;p.StartInfo.CreateNoWindow = true;p.StartInfo.FileName = serivceExeFullPath;p.StartInfo.Arguments = "stop";p.Start();p.Close();}System.Threading.Thread.Sleep(2000);this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);MessageBox.Show("服务停止成功!");}catch (Exception ex){MessageBox.Show("服务安装异常:" + ex.Message);}
}

5、服务卸载(本Windows服务是基于Topshelf框架开发)

private void btnServiceUninstall_Click(object sender, EventArgs e)
{if (!this.IsServiceExists(this.serviceName)){MessageBox.Show("服务未安装,请先安装!");return;}try{using (System.Diagnostics.Process p = new System.Diagnostics.Process()){p.StartInfo.UseShellExecute = false;p.StartInfo.CreateNoWindow = true;p.StartInfo.FileName = serivceExeFullPath;p.StartInfo.Arguments = "uninstall";p.Start();p.Close();}System.Threading.Thread.Sleep(2000);this.lblServiceStatus.Text = this.GetServiceStatus(this.serviceName);MessageBox.Show("服务卸载成功!");}catch (Exception ex){MessageBox.Show("服务安装异常:" + ex.Message);}
}

说明:对于守护程序通常是要定义检测服务的运行状态的,可以根据业务需要自己进行改进。

http://www.lbrq.cn/news/2361133.html

相关文章:

  • 可以做问答的网站关键词规划师工具
  • p2p网站如何做测试合肥网站优化平台
  • 品牌网站推广海南百度总代理
  • 寻找网站建设_网站外包昆山网站制作哪家好
  • 有什么网站可以做试题广州网站优化
  • 手机网站建设动态北京建站工作室
  • 短视频推广广告seo优化教程自学
  • 建设银行园区公积金管理中心网站网站免费搭建平台
  • 网站备案 法人应用宝aso优化
  • 学习html5的网站一个完整的策划案范文
  • 厦门建设网站哪家好网站空间租用
  • 免费签名设计上海seo有哪些公司
  • 网站界面设计策划书怎么做seo神器
  • 万全网站建设wl17581google推广一年的费用
  • 常州免费企业网站建设网络营销的特点有
  • 如何做网站新手个人教程杭州网络
  • 县政府网站加强制度建设厦门关键词优化网站
  • 南宁保洁网站建设网络营销的专业知识
  • 做电路方案设计的网站手机网站建设价格
  • 专题网站建设的请示百度网盘客服在线咨询
  • 国内代理ip地址江门seo外包公司
  • 青岛网页设计 学校seo优化技巧
  • 优设网站软文广告是什么
  • 互动的网站建设seo是什么职位的简称
  • 山东济南市网站建设手机系统优化工具
  • wordpress 手机发布seo哪家好
  • 如何自己设计一个网站自己怎么建网站
  • 昆明网站运营公司有哪些宁波抖音seo搜索优化软件
  • 抓取网站访客qq号码产品运营方案
  • 高校校园网站建设html网页制作软件
  • 从零开发足球比分APP:REST API与WebSocket的完美搭配
  • 构建足球实时比分APP:REST API与WebSocket接入方案详解
  • 16路串口光纤通信FPGA项目实现指南 - 第二部分(下)
  • 原点安全签约金网络数科,共建一体化数据安全防护体系
  • 【Java篇】IntelliJ IDEA 安装与基础配置指南
  • 基于多智能体强化学习的医疗检索增强生成系统研究—MMOA-RAG架构设计与实现