2017-02-28 59 views
0

我试图启动和终止从Windows service.Here我的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应用程序完美协作。

+0

您遇到的异常可能是由于权限不足造成的。您的目标流程是否设计为在服务环境中运行? –

+0

@indeterminatelysequenced不,如何做到这一点?而关于特权相同的东西在winform应用程序中运行完美 –

+0

您是否提供了可执行文件的完整路径? –

回答

0

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

在这种特殊情况下,您的问题看起来像是服务始终以当前目录设置为c:\windows\system32开始,而您似乎认为当前目录将设置为包含服务可执行文件的目录,当正常的应用程序从资源管理器运行时发生。

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

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