1

I am using a ProcessStartInfo to patch a file with a text file like this (through cmd.exe):

app.exe temp.txt patch.ips

I wrote this code:

ProcessStartInfo P = new ProcessStartInfo("app.exe");  
P.Arguments = "temp.txt " + _patchpath;  
P.CreateNoWindow = true;  
P.UseShellExecute = false;  
P.RedirectStandardOutput = true;  
Process.Start(P);

app.exe and temp.txt are relative to my application path (note: app.exe isn't the name of my C# application, it's just a program I'm using for the Process), but _patchpath is an absolute path like D:\blah\file.ips. The problem is, the process doesn't work (_patchpath is supposed to be patched with the file temp.txt) if its absolute, but does work if its relative to my app directory. Why is that and how can I fix it?

If I need to be clear please let me know.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
david
  • 357
  • 2
  • 7
  • 18
  • 2
    Could patch path contain spaces? If so, you'll need to enclose it in quotes. – Rob Levine Oct 20 '10 at 09:38
  • Can you provide a real path? I suspect you have a path with spaces in, and in that case you have to surround your path with quotes (") – Onkelborg Oct 20 '10 at 09:39
  • It doesn't work as in, nothing gets patched so the process doesn't work. Also, how would I use double quotes? An example would be nice. – david Oct 20 '10 at 09:47
  • any info on the redirected output ? or on the ExitCode ? they might help. – dvhh Oct 20 '10 at 13:10

2 Answers2

3

The usual approach to debug problems like this is to use the debugger. Copy/paste this into the Main() method of the source file for 'app.exe':

        System.Diagnostics.Debugger.Break();

As soon as app.exe starts running, you'll get a dialog that lets you pick a debugger. From there you shouldn't have much trouble figuring out why it doesn't work.

If you don't have the source code for app.exe then you'll need to think this through. Using a relative path like "app.exe" or "temp.txt" is always trouble. A classic failure mode is using an OpenFileDialog to let the user pick the _patchpath value. If that dialog's RestoreDirectory property isn't set to True then your program's default directory changes to the path of the patch file. And neither app.exe nor temp.txt can be fournd anymore.

Protect yourself against this by programming defensively:

        var myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
        var homeDir = System.IO.Path.GetDirectoryName(myPath);
        var appPath = System.IO.Path.Combine(homeDir, "app.exe");
        var tempPath = System.IO.Path.Combine(homeDir, "temp.txt");
        ProcessStartInfo P = new ProcessStartInfo(appPath);
        P.WorkingDirectory = homeDir;
        P.Arguments = string.Format("\"{0}\" \"{1}\"", tempPath, _patchpath);
        // etc...
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Alright, I will test the application out using the debugger (though it's written in C++ but I'm assuming I could debug it using MSVC++). And thanks for the info regarding the program's default directory changing, I didn't know that.. – david Oct 21 '10 at 12:27
  • 1
    It is __debugbreak() in MSVC. – Hans Passant Oct 21 '10 at 13:07
1

The problem is most likely that the called application (app.exe) does not understand the parameters. The best way to solve this issue would be to debug app.exe with the parameters that you provide in the case it doesn't work. Try to set the arguments in the debugger for app.exe to exactly the same parameters as the failed case, and inspect the variables that result from parsing the arguments.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111