为什么Windows服务无法启动外部应用程序?

我正在尝试使用Windows服务来启动外部应用程序。 当我启动我的服务时,它不会加载应用程序。

事件视图中也没有报告错误。 它只是表示服务已成功启动和停止。

以下是OnStart和OnStop代码:

public partial class TestService : ServiceBase { public Process App { get; set; } public TestService() { InitializeComponent(); App = new Process(); } protected override void OnStart(string[] args) { App.StartInfo.FileName = @"C:\Program Files (x86)\SourceGear\DiffMerge\DiffMerge.exe"; App.Start(); } protected override void OnStop() { App.Close(); } } 

如果您在Vista,Windows 7或Server 2008上运行,并且您的可执行文件是Windows应用程序(非命令行),则由于会话0隔离而无法运行,这意味着在最新的Windows中没有可用于服务的图形句柄OS的。

我们发现的唯一解决方法是启动RDP会话,然后在该会话中启动您的应用程序,即使这样做要复杂得多。

将此代码包含在try-catch中并添加一个小技巧 ,允许您将调试器附加到服务。 它可能是一个权限问题,但你会在catch块中得到它

 protected override void OnStart(string[] args) { Debugger.Launch(); //displays a pop up window with debuggers selection try { App.StartInfo.FileName = @"C:\Program Files (x86)\SourceGear\DiffMerge\DiffMerge.exe"; App.Start(); } catch(Exception ex) { //see what's wrong here } }