arguments – C# Problem with ProcessStartInfo – Education Career Blog

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;  

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.


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’:


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


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.

Leave a Comment