views:

536

answers:

3

Duplicate: http://stackoverflow.com/questions/240171/launching-a-application-exe-from-c/240189#240189

Hi folks,

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?

+4  A: 

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.

JP Alioto
It's worth noting that Process.Start() can accept all sorts of neat arguments. Pass it a URL and it will open the page using the default web browser. Pass it a mailto: link and it will open a new email message using the default mail client. It's very useful.
Andrew
+1  A: 

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");
Oakcool
+4  A: 

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.

Mitchel Sellers
Is this syncronous? So if i loop (where u said start loop / end loop), will it wait for the process to finish (at line Process.Start(..)) before it goes to the next line?
Pure.Krome
No, it does not wait for the process to finish, to do that you would need to put in a oStartInfo.WaitForExit(); call after the process.start if you wanted it to wait.
Mitchel Sellers
Finally, is it possible to 'chain' commands in a single 'process' ? currently i have a shortcut that runs a bat file, when i open up this special console window. it contains paths and extra 'prep' crap. Can this be called first (C:\Windows\system32\cmd.exe /K "C:\Program Files (x86)\FWTools2.3.0\setfw.bat) and then my unique program (ogr2ogr) second ... all in the same 'process'?
Pure.Krome
I would just add your individual calls then to the .bat file, and do it all at once.
Mitchel Sellers