Saturday, August 29, 2009

Displays a route between locations on a map.

/**
* Displays a route between locations on a map.
*/
private MenuItem viewRouteItem = new MenuItem("View Route" , 3000, 10)
{
public void run()
{
StringBuffer stringBuffer = new StringBuffer("");
stringBuffer.append("");
stringBuffer.append("");
stringBuffer.append("
");

Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments( MapsArguments.ARG_LOCATION_DOCUMENT, stringBuffer.toString()));
}
};

Friday, August 7, 2009

C# calls command

Example 1. Running a command line application, without concern for the results:

private void simpleRun_Click(object sender, System.EventArgs e){
System.Diagnostics.Process.Start(@"C:\listfiles.bat");
}

Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):

private void runSyncAndGetResults_Click(object sender, System.EventArgs e){
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
psi.RedirectStandardOutput =
true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute =
false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
}

Example 3. Displaying a URL using the default browser on the user's machine:

private void launchURL_Click(object sender, System.EventArgs e){
string targetURL = @http://www.duncanmackenzie.net;
System.Diagnostics.Process.Start(targetURL);
}


Using the Code

The code given below creates a process i.e. a command process and then invokes the command that we want to execute. The result of the command is stored in a string variable, which can then be used for further reference. The command execution can happen in two ways, synchronously and asynchronously. In the asynchronous command execution, we just invoke the command execution using a thread that runs independently. The code has enough comments, hence making it self-explanatory.

Below is the code to execute the command synchronously:

Collapse
/// <summary> /// Executes a shell command synchronously. /// </summary> /// <param name="command">string command</param> /// <returns>string, as output of the command.</returns> public void ExecuteCommandSync(object command) { try { // create the ProcessStartInfo using "cmd" as the program to be run, // and "/c " as the parameters. // Incidentally, /c tells cmd that we want it to execute the command that follows, // and then exit. System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); // The following commands are needed to redirect the standard output. // This means that it will be redirected to the Process.StandardOutput StreamReader. procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; // Do not create the black window. procStartInfo.CreateNoWindow = true; // Now we create a process, assign its ProcessStartInfo and start it System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); // Get the output into a string string result = proc.StandardOutput.ReadToEnd(); // Display the command output. Console.WriteLine(result); } catch (Exception objException) { // Log the exception } }

The above code invokes the cmd process specifying the command to be executed. The option procStartInfo.RedirectStandardOutput is set to true, since we want the output to be redirected to the StreamReader. The procStartInfo.CreateNoWindow property is set to true, as we don't want the standard black window to appear. This will execute the command silently.

Below is the code to execute the command asynchronously:

Collapse
/// <summary> /// Execute the command Asynchronously. /// </summary> /// <param name="command">string command.</param> public void ExecuteCommandAsync(string command) { try { //Asynchronously start the Thread to process the Execute command request. Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync)); //Make the thread as background thread. objThread.IsBackground = true; //Set the Priority of the thread. objThread.Priority = ThreadPriority.AboveNormal; //Start the thread. objThread.Start(command); } catch (ThreadStartException objException) { // Log the exception } catch (ThreadAbortException objException) { // Log the exception } catch (Exception objException) { // Log the exception } }