How to execute program with command line?

  • Thread starter Thread starter RA
  • Start date Start date
R

RA

Hi

I have a windows form app and I need to execute a program when the user
chooses an option. How do I start a program from code, with command line
arguments and getting the return value from that program?


Thanks
 
RA,

You will want to call the static Start method on the Process class,
passing the command line to the method. You can also create an instance of
the Process class and then call the Run method on it, and you should be able
to get the return value after the execution completes.

Hope this helps.
 
RA -

Did you get this to work? I'm trying something similar (calling an
executable from within C#). Unfortunately, the call to the
Process.Start() method doesn't resolve for me. Any feedback?

Terry
 
Terry,

See my message to Wade

To get an exe you can use the ProcessStartInfo.filename as well with full
path

I hope it helps,

Cor
 
Use Process.Start( program to start, attributes to pass);

program to start is a path to the .exe of the application to start
attributes to pass is a string containing attributes as You would write them
calling app manually.

You could use String builder to create attributes, something like this:

string[] args = new string[4];
args[0] = " -o";
args[1] = scriptFolder;
args[2] = "_script.txt";
args[3] = _thumbnail.sImagePath;
StringBuilder arguments = new StringBuilder();
foreach(string arg in args)
{
arguments.Append(arg + " ");
}
Process.Start(Preferences.distFix_PTPath , arguments.ToString());

or

Simply build a attribute string like this :
string arguments = " -o " + scriptFolder + " _script.txt " +
_thumbnail.sImagePath;
Process.Start(Preferences.distFix_PTPath , arguments);
 
I was able to find it in System.Diagnostics. The basic problem was
when I typed Process.Start(); I got errors. I needed the
System.Diagnostics to place before it.
 
This is a very innefficient way of coding. Have you ever thought about
writing better code?
Currently you have 4 strings in memory, which you then put into an Array,
then loop through appending them to a stringbuilder (which will copy them),
then you ToString() the stringbuilder, copying the string again to pass to
Process.Start()
As it is all hardcoded anyway you would be better off just passing a single
string to the function which would take up less memory and shorten the JIT
Compilation process.

Ciaran
 
Back
Top