Click here to Skip to main content
12,956,197 members (105,712 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 12 May 2007

Run Interactive Command Shell or Batch Files From ASP.NET

, 12 May 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
You can run an interactive command shell and run commands from a textbox with this code.

Screenshot - runcmd.jpg


This is a nice command shell that can we embedded in a webpage to run commands on the server.


I took the original code from Will Asrari's site, ao all thanks go to him:

I just made some changes to his code.

The code does not have any error handling implemented.

Using the Code

You can modify the code to run a batch file or commands directly.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
            string exec = TextBox1.Text;
            // Get the full file path
            string strFilePath = Server.MapPath("fine.bat");

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

            // Start the process
            System.Diagnostics.Process proc = 

            // Open the batch file for reading
            //System.IO.StreamReader strm = 
            //           System.IO.File.OpenText(strFilePath);
            System.IO.StreamReader strm = proc.StandardError;
            // Attach the output for reading
            System.IO.StreamReader sOut = proc.StandardOutput;

            // Attach the in for writing
            System.IO.StreamWriter sIn = proc.StandardInput;

            // Write each line of the batch file to standard input
            /*while(strm.Peek() != -1)

            // Exit CMD.EXE
            string stEchoFmt = "# {0} run successfully. Exiting";

            sIn.WriteLine(String.Format(stEchoFmt, strFilePath));

            // Close the process

            // Read the sOut to a string.
            string results = sOut.ReadToEnd().Trim();

            // Close the io Streams;

            // Write out the results.
            string fmtStdOut = "<font face=courier size=0>{0}</font>";
               results.Replace(System.Environment.NewLine, "<br>")));

Points of Interest

There can be a potentially unwanted use of this code as a trojan or as an ASP.NET hack into the server. Use it at your own risk and responsibility.


Suggestions are welcome at Any bugs or improvements will be highly appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
Hi To All

You may also be interested in...

Comments and Discussions

QuestionNot working when the project is deployed Pin
j_s_salvador5-Jun-12 13:49
memberj_s_salvador5-Jun-12 13:49 
QuestionRun Interactive Command Shell or Batch Files From ASP.NET Pin
Member 86819251-Mar-12 1:16
memberMember 86819251-Mar-12 1:16 
GeneralRequest for Info Pin
Vijay Chandra Sekhar Parepalli23-Mar-08 16:38
memberVijay Chandra Sekhar Parepalli23-Mar-08 16:38 
GeneralExtra Ordinary Pin
hackrogenius28-Sep-07 23:18
memberhackrogenius28-Sep-07 23:18 
GeneralNice Pin
Will Asrari8-Jun-07 12:23
memberWill Asrari8-Jun-07 12:23 
QuestionKudo's / Question / Bug??? Pin
sides_dale19-May-07 10:55
membersides_dale19-May-07 10:55 
Generalsmall bug Pin
margiex13-May-07 3:46
membermargiex13-May-07 3:46 
GeneralRe: small bug Pin
darkalchemy13-May-07 21:17
memberdarkalchemy13-May-07 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 13 May 2007
Article Copyright 2007 by darkalchemy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid