Soatl
Soatl

Reputation: 10592

Web Application Asp.Net: Execute a Dos Command

I have a web application where I want to send commands to a command line (commands are not known). This is the method I use

    public static string ExecuteCommand(string command)
    {
        String result;
        try
        {
            //Create a new ProcessStartInfo
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
            //Settings
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;
            //Create new Process
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            //Set ProcessStartInfo
            proc.StartInfo = procStartInfo;
            //Start Process
            proc.Start();
            //Wait to exit
            proc.WaitForExit();
            //Get Result
            result = proc.StandardOutput.ReadToEnd();
            //Return
            return result;
        }
        catch
        {
        }
        return null;
    }

The command works on a console application but not on a web application (null is returned)

    public string test = "NOTHING";

    protected void Page_Load(object sender, EventArgs e)
    {
        test = AppCommandHandler.ExecuteCommand("mkdir test2");
    }

What am I doing wrong? Every tutorial I look at tells me to use ProcessStartInfo

Edit I keep getting this error:

{"StandardOut has not been redirected or the process hasn't started yet."}

Upvotes: 2

Views: 6809

Answers (2)

Sohail xIN3N
Sohail xIN3N

Reputation: 3041

You cannot do that like this but you've to do this with using java directly, just see the link:

Run a cmd command with asp dotnet app

Upvotes: 0

Leon
Leon

Reputation: 3401

Does the web app's pool have enough permissions to execute this command?

Upvotes: 2

Related Questions