c#守护进程(windows服务监测程序,程序关闭后自启动)最详细!!!_...

c#守护进程(windows服务监测程序,程序关闭后自启动)最详细!!!_...

2023年6月23日发(作者:)

c#守护进程(windows服务监测程序,程序关闭后⾃启动)最详细最近项⽬需要:程序关闭后⾃动重新启动,需要⼀个监测程序所以写下这篇⽂章,为⾃⼰以后留个印象,也给⼤家⼀个参考,不喜勿喷1.打开VS创建windows服务 2.实现服务的操作步骤(查看service1代码) 3.(右键)添加引⽤(这个dll是为显⽰界⾯的,很多⼈说windows服务开启了,程序也在运⾏就是不显⽰界⾯那就需要这个了) 记得添加命名空间引⽤dll需要⽤到的代码 try { //appStartPath = "程序路径"; IntPtr userTokenHandle = ; ryUserToken(ActiveConsoleSessionId(), ref userTokenHandle); S_INFORMATION procInfo = new S_INFORMATION(); PINFO startInfo = new PINFO(); = (uint)(startInfo); ProcessAsUser( userTokenHandle, appStartPath, "", , , false, 0, , null, ref startInfo, out procInfo); if (userTokenHandle != ) andle(userTokenHandle); int _currentAquariusProcessId = (int)essId; } catch (Exception ex) { }4.在对应位置写⼊代码protected override void OnStart(string[] args) { //服务开启执⾏代码 } protected override void OnStop() { //服务结束执⾏代码 } protected override void OnPause() { //服务暂停执⾏代码 e(); } protected override void OnContinue() { //服务恢复执⾏代码 inue(); } protected override void OnShutdown() { //系统即将关闭执⾏代码 down(); }全部代码using System;using c;using entModel;using ;using stics;using ;using eProcess;using ;using ;using ;using sApi;namespace process{ public partial class Service1 : ServiceBase { string appStartPath = @"C:"; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { // string appStartPath= @"C:"; timer;

timer = new (); al = 10000;//设置计时器事件间隔执⾏时间 d += new dEventHandler(circulation); d = true; } protected override void OnStop() { } private void circulation(object sender,dEventArgs e) { try try { //appStartPath = "程序路径"; IntPtr userTokenHandle = ; ryUserToken(ActiveConsoleSessionId(), ref userTokenHandle); S_INFORMATION procInfo = new S_INFORMATION(); PINFO startInfo = new PINFO(); = (uint)(startInfo); ProcessAsUser( userTokenHandle, appStartPath, "", , , false, 0, , null, ref startInfo, out procInfo); if (userTokenHandle != ) andle(userTokenHandle); int _currentAquariusProcessId = (int)essId; } catch (Exception ex) { } string appName = "okl1";//the path of the exe file bool runFlag = false; Process[] myProcesses = cesses(); foreach (Process myProcess in myProcesses) { if (eTo(appName) == 0) { runFlag = true; } } if (!runFlag) //如果程序没有启动 { Process proc = new Process(); me = appName; gDirectory = ectoryName(appStartPath); (); } } }}5.添加安装程序在services1的设计界⾯右键,选择添加安装程序:⽣成serviceInstaller1和 serviceProcessInstaller1两个组件 。6.把serviceInstaller1的属性ServiceName改写为你的服务程序名,并把启动模式设置为AUTOMATIC7.把serviceProcessInstaller1的属性account改写为 LocalSystem8.通过从⽣成菜单中选择⽣成来⽣成项⽬安装卸载Windows服务现在你所需要的代码写完之后点击运⾏是运⾏不了的9.安装windows服务存在路径为:C:版本号2.找到它把它复制到你的项⽬中的binDebug或者binRelease下3.打开cmd输⼊命令runas /user:Administrator cmd 输⼊密码(不知道的⾃⼰百度吧)4.获得更⾼权限,避免后续权限不够出问题5.输⼊安装命令⽐如:C:Windowssystem32> 空格 服务名.exe6.安装成功之后:控制⾯板>管理⼯具>计算机管理>服务和应⽤程序>服务>找到你的服务开启就ok了卸载Windows服务输⼊命令:C:Windowssystem32> 空格 服务名.exe/u 就是多了个/u如果修改这个服务,但是路径没有变化的话是不需要重新注册服务的,直接停⽌服务,然后⽤新的⽂件覆盖原来的⽂件即可,如果路径发⽣变化,应该先卸载这个服务,然后重新安装这个服务。转载请注明出处

发布者:admin,转转请注明出处:http://www.yc00.com/web/1687518521a16450.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信