How to run a command line program in your program

There are times when we need to kickoff another console application within our program.

Recently, I found I needed to kickoff a few little jobs on a remote server. I considered putting them on a scheduler but that didn’t make sense. The programs I am dealing with are needed once or twice a month and not at the same time each month. I also have the requirement that they can be triggered when needed by other departments.

What I have running on the remote server is a scheduled up executable runner as my watcher. I have an internal application that users are using to pas job requests to a database and then the watching executable runner on the remote server is picking them up and kicking off the small jobs on that same server.

There are a few libraries that “offer” this functionality but in my opinion they don’t work effectively and aren’t worth the overhead. The command to kick off a cmd.exe program is actually fairly simple.

Starts a process resource and associates it with a Process component.

More information about this method can be found here.

When trying to open a program in cmd.exe you are going to pass a couple of arguments.

  1. Pass CMD.exe
  2. Pass arguments to CMD.exe. These are your program arguments:
    • “[window option]
    • [path to the target exe folder]
    • [the exe itself]
    • [any arguments for the exe to have]”

When finished we’ll have something like:

Process.Start("CMD.exe", "/C  C:\Users\Me\My.exe --dev a,b,e");

The window option /C tells the cmd.exe window to close once the process is complete. I use this because I don’t want a bunch of windows cluttering the server when in use, but I do like them to open in case I feel like looking while something is running.

That’s it, pretty simple right?

Leave a Reply

Your email address will not be published. Required fields are marked *