Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

Execute the Batch files i.e. .Bat from ASP.Net

March 7 2009 , Written by Balavardhan Published on #ASP.Net

Execute the Batch files from c#, ASP.Net

Required Dotnet namespace to to Run the Batch file is
using System.Diagnostics

Create a process and ProcessStartInfo objects


Process p = null;


We need Command prompt to Execute or Run th Batch files. so provide cmd.exe as file name for ProcessStartInfo object


ProcessStartInfo pti = new ProcessStartInfo("cmd.exe");


Below properties you needs set ProcessStartInfo


pti.WorkingDirectory = Server.MapPath("~/Script");
pti.ErrorDialog= true;
pti.CreateNoWindow = false;
pti.RedirectStandardOutput= true;
pti.RedirectStandardInput=true;
pti.UseShellExecute = false;


Set the Startinfo Object to Process and start the Process

Process p = new Process();
p.StartInfo = pti;
p.Start();


Read the Batch file through the StreamReader object

StreamReader strm = File.OpenText(Server.MapPath("~/Script/Temp.BAT"));
String strCommand = strm.ReadToEnd();


Then write the Input under Process i.e. Process is calling Cmd.exe, so we need to write command prompt text in cmd.exe

p.StandardInput.WriteLine(strCommand);

Here strCommand has the Batch file code, so we sending strCommand as Input to process.

p.StandardInput.WriteLine("exit");

Exit from the cmd.exe and close the process

p.Close();


If you sending any parameters to batch file, then your batch file code like
Ex :
copying filesfrom location to another location, and you need to send locations as parameters

copy %1\*.* %2
Here %1 and %2 are batch file parameters.

Now we need to set these parameters to Batch file from the ASp.Net application

Solution:


StreamReader strm = File.OpenText(Server.MapPath("~/Script/Temp.BAT"));
String strCommand = strm.ReadToEnd();


By the above code we get the Batch file code into string, here you just replace the %1 and %2 values of the Batch file code with your values
Ex:


strCommand = strCommand.Replace("%1",c:\Temp);
strCommand = strCommand.Replace("%2",c:\Temp_new);


by the above replace your batch file code will modifies as
copy c:\Temp\*.* c:\Temp_new
so when the process runs, it will copy the files from Temp to Temp_new


Ex :

Batch file : Create a a batch file with below code and name as Temp.bat and store in Project folder

copy %1\*.* %2

Process p = null;
ProcessStartInfo pti = new ProcessStartInfo("cmd.exe");
Process p = new Process();
pti.WorkingDirectory = Server.MapPath("~/");
pti.ErrorDialog= true;
pti.CreateNoWindow = false;
pti.RedirectStandardOutput= true;
pti.RedirectStandardInput=true;
pti.UseShellExecute = false;
p.StartInfo = pti;
p.Start();
StreamReader s = File.OpenText(Server.MapPath("~/Temp.bat"));
string strCommand = s.ReadToEnd();
strCommand = strCommand.Replace("%1",c:\Temp);
strCommand = strCommand.Replace("%2",c:\Temp_new);
p.StandardInput.WriteLine(strCommand);
p.StandardInput.WriteLine("exit");
p.Close();

Share this post

Repost 0

Comment on this post