Running a Shell Script via ASP.Net

Posted by on May 8th, 2008
2008
May 8
This past week I’ve been trying to run a couple of batch commands via ASP.Net they run perfectly fine on my box but when it was released to IIS 6.0 Windows 2003 server it never worked.

So I made a couple of experiments and lot of googling and ended up with the following:

My original code was

ProcessStartInfo p = new ProcessStartInfo("C:\blahblah.cmd');
Process proc = Process.Start(p);
proc.WaitForExit(5000); //wait for 5 secs then exit
proc.Close();


My new code became

ProcessStartInfo p = new ProcessStartInfo("cmd.exe");
p.RedirectStandardOutput = true;
p.RedirectStandardInput = true;
p.RedirectStandardError = true;

Process proc = Process.Start(p);
StreamReader reader = File.OpenText("C:\blahblah.cmd');
StreamWriter writer = proc.StandardInput;
while(reader.Peek() != -1)
{
   writer.WriteLine(reader.ReadLine());
}
writer.Close();
reader.Close();
proc.Close();


And it worked like a charm.

Thanks to Brendan Tompkins for the idea check his solution here,

Leave a Comment




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.