How to launch a command line app from CLR


Just saw this one fly by on an internal list and thought google might help someone find it someday:

Question: I have “myapp.exe a b” to be executed. How can I launch such command with CLR API?

Answer (a local smart guy): System.Diagnostics.Process.Start(“myapp.exe”, “\”a\” \”b\””);

Comments (10)

  1. Wes says:

    I don’t believe you need the inner quotes. It should work fine like System.Diagnositics.Process.Start("myapp.exe", "a b"); If you pass it with quotes it would be equivalent to "myapp.exe "a" "b"".

    Wes

  2. Nate says:

    I’ve always found the .NET framework to be extremely well designed, but putting Process in System.Diagnostics seemed to me to be extremely wacky. I know that the placement was inherited from its association with PSAPI.DLL, but that is still lame.

  3. Jelle Druyts says:

    I agree on both previous comments 😉 I don’t see the need for the extra quotes and there’s something weird about Process not living in e.g. System.Runtime or System.Threading.

    Related to the Process.Start call: it gets fishy if you have a full commandline you want to run but not the exe name and the arguments separately. I came across this when trying to launch the default email client (see http://jelle.druyts.net/PermaLink.aspx?guid=b8e15df1-5948-4f87-a976-b858a65c9baa): I built in some basic argument extractor but it’s not really clean since it just checks for a space – which would be incorrect if the exe name is quoted and contains spaces. Point being: there should be a Process.Start(string fullCommandLine) overload 🙂

  4. Jelle Druyts says:

    Whoops, scratch §2 in the previous comment: that wouldn’t be possible of course since that method signature is already used for launching a process without any arguments. So I would opt for a "static ProcessStartInfo Parse(string fullCommandLine)" method on ProcessStartInfo. That would simply allow you to do

    Process.Start(ProcessStartInfo.Parse(fullCommandLine));

  5. You can also use (and I prefer) the startInfo class and use the arguments property of it.

    process.startInfo.arguments = "arguments here"

    process.start()

  6. Wes says:

    Jelle,

    I had to write a very similar command line argument parsing function for my new program run++ (http://weblogs.asp.net/whaggard/archive/2004/09/19/231576.aspx). I eventually plan on releasing the source code for it.

  7. I really hate System.Diagnostics not being in the .NET Compact Framework – it’s always a little painful to see CF code have to recreate the Process.Start code with P/Invoke’s. It’s a cut I can understand, but I don’t have to like it 🙂

  8. Good news for you James… I just learned that this will be in V2.0 of the Compact Framework. It did not make it until Beta1, but it will be in Beta2 and RTM…

  9. Anton Spass says:

    Brad,

    It looks like [ProcessStartInfo.RedirectStandardOutput] flag doesn’t work while used within Windows Service.

    While writing Subversion backup service i’ve experienced that following code fills [backupData] string while called whithin Console app but produces no output/Exceptions while called within Windows Service (used context of admin user):

    ProcessStartInfo psi = new ProcessStartInfo();

    psi.FileName = svnAdminPath;

    psi.Arguments = arguments;

    psi.UseShellExecute = false;

    psi.RedirectStandardOutput = true;

    string backupData = string.Empty;

    try

    {

    Process process = Process.Start(psi);

    backupData = process.StandardOutput.ReadToEnd();

    process.WaitForExit();

    }

    catch (Exception ex)

    {

    EventLog.WriteEntry("SvnBackupSource", ex.Message, EventLogEntryType.Error);

    }

    Could you please provide some comments on the issue?

Skip to main content