0

I am trying to Start and Kill my wpf application from a windows service.Here what I used to start the application :

Process.Start("MyApplicationName");

It gives me the following exception here :

> 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)

And while killing the process it gives me the following exception

> 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)

While the same piece of code works perfectly with Winform application.

1 Answers1

0

The current (or "working") directory is a legacy concept, useful for simple command-line tools but otherwise best avoided.

In this particular case, it looks like your problem is that services are always started with the current directory set to c:\windows\system32 whereas you appear to be assuming that the current directory will be set to the directory containing the service executable, as would happen when a normal application is run from Explorer.

Your code should explicitly locate the directory containing the service executable and use that to build the path to the child executable. See this question for more information.

If the child executable also assumes that the current directory is going to be the same as the application directory, you can use Process.StartInfo.WorkingDirectory to select the current directory that will be assigned to the child.

Community
  • 1
  • 1
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158