【发布时间】:2019-04-23 17:42:38
【问题描述】:
我正在尝试创建一个 Windows 服务来管理启动多个 exes/bat 文件。
到目前为止,我已经关注了这个guide,并且能够使用以下代码启动一个 exe。但是当我停止服务时,生成的 exe 似乎已分离,我不确定如何以编程方式杀死它。
最终我想启动和停止许多不同的bats 和exes。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Runtime.InteropServices;
namespace SomeService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Process.Start("C:\\Users\\JohnnySmalls\\SomeProgram\\bin\\theExe.exe");
}
protected override void OnStop()
{
// Need to close process, I don't have a reference to it though
// Process.Close();
}
}
}
【问题讨论】:
-
Process.Start()返回一个 Process 对象。存储它,然后.CloseMainWindow()(也与控制台相关)或.Kill()它。我怀疑.Close()会起作用。 -
@Jimi 谢谢你似乎成功了!如果我使用正确的模式来启动/停止多个 exes/bats,仍在寻找反馈。
-
它会起作用,但它可能不是最好的方法。请参阅此代码项目:Subverting Vista UAC in Both 32 and 64 bit Architectures。它有一些有用的信息。它已经过时了,一些注意事项不再适用,但你可以找到一些有趣的东西。看这里的主要功能:CreateProcessAsUser
-
我不需要颠覆 UAC 或显示 GUI。
-
不,你没有。我告诉过你它很旧。这是关于 CreteProcessAsUser 函数的。