Pages

Jul 1, 2010

How to pass double couted paramaters to Command line utilities programatically + c#

Invoking a Command Line Utility From C#:


For eg. let's consider FWTools which has a command line utility named 
ogr2ogr for converting tab to kml
Below is the code to invoke the command line utility ogr2ogr

Invoking Command Line without parameters:

// Create an instance for System.Diagnostic.Process
Process proc = new Process();
// Provide the utility name with location
proc.StartInfo.FileName = @"C:\FWTools2.4.7\bin\ogr2ogr.exe";
// Start the process
proc.Start();

Invoking Command Line with normal parameters:
Note: -f, KML, sourcepath, destpath are arguments separted by space
Process proc = new Process(); 
string driver = @"-f KML C:\FWTools2.4.7\bin\zeeee.kml 
C:\FWTools2.4.7\bin\Morocco.tab"
proc.StartInfo.FileName = @"C:\FWTools2.4.7\bin\ogr2ogr.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.Arguments = driver; 
proc.Start();
proc.WaitForExit(); // use if you want your  program to wait for the process to complete.
proc.Close();

Invoking Command Line with Double couted parameters:

Usually space in the arguments list denote that each are separate
argument separated by space.

In the above code KML is the name of the driver used to convert 
Tab to KML.

And to convert ESRI's shape file to MapInfo's Tab file the driver 
used is "MapInfo File", which itself has a space

so by default command line arguments will consider it as a
two parameter one is "MapInfo" and other is "File"
so the program goes wrong.

Below is the solution, how to pass the double couted paremeter

string driver = "-f " + @"""" + "MapInfo File" + @"""" +
 @" C:\FWTools2.4.7\bin\test.tab C:\FWTools2.4.7\bin\test.shp";

By using this methodology, we can pass "MapInfo File" as a 
single argument to a command line utility.

No comments: