3

I have a program that watches processes and restarts them if they close or fail. It started as a work project but I took it home to keep game servers up on my home server, i.e. Minecraft, Terraria and more recently Cube World. The program is standalone and sits on my desktop. When it starts Cube World's Server.exe, all of the files that the exe would normally create in Cube World's folder are created on the desktop. My guess is that my program has a working directory of the desktop so any child processes it starts (like Server.exe) will have the same working directory. The problem is not specific to just Cube World's server at home. This program plays keep alive with important back end processes at work.

If I'm starting a Process in C# using ProcessStartInfo how can ensure that the working directory of the started process is the directory the exe is in? The Process may be started with a relative path name or may be on the System PATH making this a little more difficult.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188

1 Answers1

3

Use ProcessStartInfo.WorkingDirectory in combination with Check if an executable exists in the windows path to determine the path to the executable.

You might need to first check for relative paths, which I think should also handle absolute paths (I have not tested the following code, but it looks ok to me):

string myAppPath = System.Reflection.Assembly.GetEntryAssembly().Location;
if (File.Exists(Path.Combine(myAppPath, pathToExe)))
{
    workDir = Path.GetDirectoryName(Path.Combine(myAppPath, pathToExe));
}
else 
{
    // Use the referenced article to iterate thru System PATH to find the right path
}
Community
  • 1
  • 1
Michael Bray
  • 14,998
  • 7
  • 42
  • 68
  • Per your edit, I'd rather use `Path.GetFullPath(pathToExe)` than append my own app's path. But I see where you're going and may toy with it for a bit. I want to be sure before I change the code and get a truly obscure bug. – Corey Ogburn Jul 09 '13 at 01:54
  • @CoreyOgburn: yup I think that would be fine - I forgot about that function (it's exactly the result I was trying to get out of `Path.Combine`) – Michael Bray Jul 09 '13 at 01:58
  • I just want to avoid a situation where my program is on the path and the working directory is not my app's path or some other strange scenario where my program finds one path and when I start the process, it runs from another. That'd be worse than things are now. – Corey Ogburn Jul 09 '13 at 02:00
  • @CoreyOgburn: Sure of course... But neither my code nor `GetFullPath` will guarantee that. What will guarantee that is to determine the full path to the EXE (your way or my way) and then start that specific EXE by specifying the full path in `ProcessStartInfo.FileName`. Setting `ProcessStartInfo.WorkingPath` is a good idea but actually has nothing to do with exactly which EXE gets started. – Michael Bray Jul 09 '13 at 02:13
  • For anybody who may find this in the future: I also discovered that I sometimes needed to pay attention to PATHEXT in order to find the file. – Corey Ogburn Jul 09 '13 at 16:34