Search This Blog

Tuesday, March 11, 2014

How to run another application or batch file from C# .NET

 To  run another application or batch file from C# .NET

In .net framework there is class given System.Diagonostic.Process by using this class we can do the same
as we do with run command in windows, Like to start MS Word we just do Start->Run->Type winword
this will open MS Word

In Same way we Just write the code to open MS word on a button click:

System.Diagonostic.Process.Start("winword");

same way open Notepad we can write as

System.Diagonostic.Process.Start("notepad");

to run a batch file 

System.Diagnostics.Process.Start(@"C:\abc.bat");
Retrieve the results and waiting until the process stops (running the process synchronously): 
  
private void btnGetSync_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;
 }
}

We can also launch a specified url using 

string urURl="www.dotnethope.blogspot.com";
System.Diagnostics.Process.Start(urURl);



No comments:

Post a Comment