advanced web statistics

Run a Command Shell Executable from ASP.NET

8/2/2006 11:34:14 AM

PLEASE STOP HARASSING ME ABOUT THIS CODE! IT IS MERELY A REPOST OF SOMEONE ELSE'S CODE AND IS THE BASIS OF MY ENTIRELY DIFFERENT SOLUTION.

What a pain. I`ve spent about 3 days trying to figure out how to run command shell executables from an ASP.NET solution and it drove me completely insane.  I knew that System.Diagnostics.Process was going to be used. Check.  Since I have never started a process from a .NET application I browsed for some sample code from MSDN.

After many unsuccessful attempts of opening Notepad.exe I tried the next logical thing: opening a command shell (cmd.exe). Success! I then stumbled upon Brendan Tompkins` blog and a tutorial entitled "Run a .BAT File from ASP.NET" along with some sample code.  If you are able to successfully open cmd.exe from your application then you should simply create a text file (.txt, .bat, etc..)with command line(s), open cmd.exe and use a streamwriter to write out the line.  It worked like a charm!

I also think that it is worth mentioning that you should concatenate an Environment.NewLine (or VB equivalent) where you write out the line from the text file. This will act as a carriage return.  When I ran the code without using the newline it didn`t appear to work, and did as soon as I added the return.  But this may just be me.

Code Snippet
string filepath = Server.MapPath("import/ga_import.bat");

// Create the ProcessInfo object
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;

// Start the process
Process proc = Process.Start(psi);
StreamReader sr = File.OpenText(filepath);
StreamWriter sw = proc.StandardInput;

while(sr.Peek() != -1)
{
   // Make sure to add Environment.NewLine for carriage return!
   sw.WriteLine(sr.ReadLine() + Environment.NewLine);
}

sr.Close();
proc.Close();
sw.Close();

Quasi-Important Note
Usually (at least in my experience) console applications should be pretty straight-forward with minimal bells and whistles.  The executable I am dealing with has 2 alert boxes when it is ran. The first tells you that it is going to run and prompts you (OK / Cancel).  The second lets you know that it is closing down.  While running the code from above I would hear that annoying error beep. There is a very simple solution for this.

Original
\\server\exec\file.exe /Argument1=1 /Argument2=2

Edited
\\server\exec\file.exe /GO /Argument1=1 /Argument2=2

You can also execute a command without loading a .bat or .txt file.  In my case I stored the arguments and server name in my web.config file, created a command string at run time, and then used the StreamWriter to write out a line. Easy.

RUNNING A .BAT IN AN INTERNET SETTING IS DANGEROUS AND SHOULD BE DONE WITH DISCRETION. MY SOLUTION (EVEN THOUGH IT DID NOT USE A .BAT FILE) WAS FOR AN INTRANET APPLICATION.

Related Links:
Brendan Tompkins - Run a .BAT File from ASP.NET
Unable to Start a Process from ASP.NET

.NET, C#, Code, Interesting, Programming

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!