Windows services 如何从windows服务启动和终止进程

Windows services 如何从windows服务启动和终止进程,windows-services,Windows Services,我正在尝试从windows服务启动和终止我的wpf应用程序。以下是我用来启动该应用程序的内容: Process.Start("MyApplicationName"); 这给了我以下例外情况: > The system cannot find the file specified;; at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnos

我正在尝试从windows服务启动和终止我的wpf应用程序。以下是我用来启动该应用程序的内容:

Process.Start("MyApplicationName");
这给了我以下例外情况:

> The system cannot find the file specified;;   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
在终止进程的同时,它给了我以下异常

> The system cannot find the file specified;;   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at SSMScoreboardService.Service1.Scoreboard(String status)
而同一段代码在Winform应用程序中也能完美地工作。

当前(或“工作”)目录是一个遗留概念,对于简单的命令行工具很有用,但最好避免使用

在这种情况下,您的问题似乎是服务总是在当前目录设置为
c:\windows\system32
的情况下启动,而您似乎假设当前目录将设置为包含服务可执行文件的目录,当从资源管理器运行普通应用程序时,会发生这种情况

您的代码应该显式定位包含服务可执行文件的目录,并使用该目录构建子可执行文件的路径。有关更多信息,请参阅


如果子可执行文件还假定当前目录与应用程序目录相同,则可以使用
Process.StartInfo.WorkingDirectory
选择将分配给子文件的当前目录。

您获得的异常可能是由于权限不足。您的目标进程是否设计为在服务上下文中运行?@indeterinelysequenced否,如何做到这一点?关于权限,同样的东西在winform应用程序中运行得很好。你提供了可执行文件的完整路径吗?@HarryJohnston没有,但我把它保存在我的工作目录中