我怎样才能运行Windows服务使用C#中的EXE程序?程序、Windows、EXE

2023-09-02 01:26:31 作者:怼死智障队友

这是一个Windows服务使用C#我怎样才能运行的EXE程序?

How can I run an EXE program from a Windows Service using C#?

这是我的code:

System.Diagnostics.Process.Start(@"E:PROJECT XLINI SQLLOADERConsoleApplication2ConsoleApplication2ConsoleApplication2binDebugConsoleApplication2.exe");

当我运行此服务,该应用程序没有启动。请告诉我什么地方错了我的code。谢谢你。

When I run this service, the application is not starting. Please tell me what's wrong with my code. Thanks.

推荐答案

这行不通,至少不会在Windows Vista或更高版本。关键的问题是,你想从Windows服务中执行这一点,而不是一个标准的Windows应用程序。还有你的code将工作完全在Windows窗体,WPF,或者控制台应用程序,但它不会在Windows服务在所有的工作。

This will never work, at least not under Windows Vista or later. The key problem is that you're trying to execute this from within a Windows Service, rather than a standard Windows application. The code you've shown will work perfectly in a Windows Forms, WPF, or Console application, but it won't work at all in a Windows Service.

Windows服务无法启动​​其他应用程序。不同于常规的Windows应用程序,服务现在在一个孤立的会话运行,并从互动被禁止与用户或桌面。这使得应用程序要运行的地方。

Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are now run in an isolated session and are prohibited from interacting with a user or the desktop. This leaves no place for the application to be run.

更多信息,请访问:

How可以在Windows服务启动一个进程时,计时器事件引发? which过程窗口是特定的用户? windows服务(允许服务与桌面交互) How can a Windows Service start a process when a Timer event is raised? which process in windows is user specific? windows service (allow service to interact with desktop)

你的问题的最佳解决方案,因为你可能已经想通了,现在,是创建一个标准的Windows应用程序,而不是一种服务。这些被设计成由特定用户来运行,并与该用户的桌面相关联。这样,您就可以运行,每当你想更多的应用程序,使用您已经展示了code。

The best solution to your problem, as you've probably figured out by now, is to create a standard Windows application instead of a service. These are designed to be run by a particular user and are associated with that user's desktop. This way, you can run additional applications whenever you want, using the code that you've already shown.

另一种可能的解决方案,假设你的控制台应用程序不需要任何形式的接口或输出,是指示过程中没有创建一个窗口。这将prevent Windows无法阻挡你的创作过程中,因为它将不再要求创建一个控制台窗口。你可以找到相关的code在this回答以一个相关的问题。

Another possible solution, assuming that your Console application does not require an interface or output of any sort, is to instruct the process not to create a window. This will prevent Windows from blocking the creation of your process, because it will no longer request that a Console window be created. You can find the relevant code in this answer to a related question.