ASP.NET - query - Asked By on 06-Jan-12 05:40 AM

How we run batch file in c#?
Jitendra Faye replied to on 06-Jan-12 05:43 AM

private void simpleRun_Click(object sender, System.EventArgs e)

Web Star replied to on 06-Jan-12 05:44 AM

you can use the classes and methods in to accomplish these tasks and more.

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

private void simpleRun_Click(object sender, System.EventArgs e){

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;
 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 = @;
Suchit shah replied to on 06-Jan-12 05:45 AM
you can just do it like below code one

System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"c:\foo.bat") ;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;

kalpana aparnathi replied to on 06-Jan-12 05:50 AM

Batch files don't support UNC paths as their "current directory". There's a hackish work around of doing:

pushd "%~dp0"
your batch stuff

%~dp0 expands to the current (d)rive/(p)ath/(0)batchfilename


ok. a Simple batch file:

pushd %~dp0
echo "Hello from batch land"
echo %~dp0

put that on a server somewhere, and try to run it via a unc path:

C:\> \\server\share\test.bat

You'll get as output:

C:\>pushd \\server\share\

Z:\>echo Hello from batch land
Hello from batch land

Z:\>echo \\server\share\


aman replied to on 06-Jan-12 05:54 AM
import system.diagnostic
then use 
process.start("your application name/ address");
like this
Sri K replied to on 06-Jan-12 06:46 AM
It works fine. I tested it like this:

String command = @"C:\Doit.bat";

ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
// ProcessInfo.CreateNoWindow = true;