Pure.Krome
Pure.Krome

Reputation: 87087

Can a WinForm execute a console program? If so, how?

I have a free command line tool called FW Tools. Works great. Here is a sample line i enter in the console window :-

ogr2ogr -f "ESRI Shapefile" -a_srs "EPSG:26986" -t_srs "EPSG:4326"
    towns_geodetic.shp TOWNSSURVEY_POLY.shp

I wish to change the last to arguments, based on some list i dynamically generate (i use Linq-to-Filesystem and grab all the filenames) and then call this program 'n' times.

I don't care about the output.

Can this be done?

This is all under the .NET environment btw.

EDIT

Is there also any way to make sure the code waits for the process to finish?

Upvotes: 1

Views: 2869

Answers (3)

Mitchel Sellers
Mitchel Sellers

Reputation: 63136

I would take a bit more of a detailed approach to this and do something like this

string ApplicationName = "ogr2ogr";
string BaseOptions = "-f \"ESRI Shapefile\" -a_srs \"EPSG:26986\" 
                      -t_srs \"EPSG:4326\"";

//Start your loop here
ProcessStartInfo oStartInfo = new ProcessStartInfo()
oStartInfo.FileName = ApplicationName;
oStartInfo.Arguments = BaseOptions + MyLoopLoadedItemsHere;
Process.Start(oStartInfo)

//If you want to wait for exit, uncomment next line
//oStartInfo.WaitForExit();

//end your loop here

Something like this is a bit more readable, at least in my opinion.

Upvotes: 6

Oakcool
Oakcool

Reputation: 1496

Here we go my friend

Process.Start(@"C:\Windows\notepad.exe", @"C:\Windows\system.ini");

or

System.Diagnostics.Process.Start(@"C:\Windows\notepad.exe", 
                                 @"C:\Windows\system.ini");

Upvotes: 1

JP Alioto
JP Alioto

Reputation: 45127

Use Process.Start. Something like ...

Process.Start( "cmd /c Gregory -f \"ES RI Shape file\" 
      -a_Sirs \"PEGS:26986\" -t_Sirs \"PEGS:4326\"
      towns_geodetic.Shep TOWNS SURVEY_PLOY.Shep" );

Here are some examples of how to do it a bit cleaner.

Upvotes: 5

Related Questions