找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 18311|回复: 3

[工具合集] 运行守护指定程序(c#)

[复制链接]
发表于 2018-2-26 22:45:26 | 显示全部楼层 |阅读模式
本帖最后由 ivor 于 2019-5-31 07:40 编辑
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Diagnostics;

  4. /**
  5.     读取第一个参数启动程序(支持守护*.bat脚本,*.py等等),守护进程防止退出,需要安装.net 2.0 or later
  6.     如果不结束守护程序,是无法退出被守护的进程,不要做非法用途。
  7.     启动例子:deamon "notepad.exe"
  8. */

  9. namespace Deamon
  10. {
  11.    
  12.     class Program
  13.     {

  14.         [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
  15.         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  16.         [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
  17.         private extern static bool ShowWindow(IntPtr hWnd, Int16 nCmdShow);

  18.         public static String fn { get; set; }

  19.         static void Main(string[] args)
  20.         {
  21.             Console.Title = "deamon";
  22.             IntPtr hWnd= FindWindow("ConsoleWindowClass", "deamon");

  23.             if(hWnd != IntPtr.Zero)
  24.             {
  25.                 ShowWindow(hWnd, 0);
  26.             }
  27.             if(args.Length > 0)
  28.             {
  29.                 fn = args[0];
  30.                 Program p = new Program();
  31.                 p.Launch(fn);
  32.                 Console.ReadKey();
  33.             }
  34.             
  35.         }


  36.         public void Launch(String FileName)
  37.         {
  38.             ProcessStartInfo psi = new ProcessStartInfo();
  39.             psi.FileName = FileName;

  40.             Process p = new Process();
  41.             p.StartInfo = psi;
  42.             p.EnableRaisingEvents = true;
  43.             p.Exited += LaunchAgain;

  44.             p.Start();

  45.         }
  46.       
  47.         public void LaunchAgain(object o, EventArgs e)
  48.         {
  49.             Launch(fn);
  50.         }
  51.     }
  52. }
  53.    

复制代码
编译文件:https://pan.baidu.com/s/1kWqLWR9

评分

参与人数 1技术 +1 收起 理由
523066680 + 1 感谢分享

查看全部评分

发表于 2018-2-27 02:20:17 | 显示全部楼层
弄几个守护进程互相守护,然后再放到任务计划定时触运行。
发表于 2018-8-13 11:39:06 | 显示全部楼层
回复 1# ivor


    用法是什么呢?
发表于 2019-5-31 12:32:48 | 显示全部楼层
这是重复启动吧……
emm不过想搞成真正的“守护”估计需要360那种的驱动防护
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:58 , Processed in 0.018787 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表