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

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

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

{
 System.Diagnostics.Process.Start(@"C:\listfiles.bat"
);
}
Web Star replied to myzonal.com myzonal.com on 06-Jan-12 05:44 AM

you can use the classes and methods inhttp://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessClassTopic.asp 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){
 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);
}
Suchit shah replied to myzonal.com myzonal.com 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;

proc.Start();
proc.WaitForExit();
kalpana aparnathi replied to myzonal.com myzonal.com on 06-Jan-12 05:50 AM
hi,

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

pushd "%~dp0"
your batch stuff
popd

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


example:

ok. a Simple batch file:

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

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\
\\server\share\

Z:\>popd

C:\>
aman replied to myzonal.com myzonal.com on 06-Jan-12 05:54 AM
import system.diagnostic
then use 
process.start("your application name/ address");
like this
process.start("calc.exe");
process.start("winword.exe");
Sri K replied to myzonal.com myzonal.com 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;