2

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."}

Soatl
  • 10,224
  • 28
  • 95
  • 153

2 Answers2

2

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

Leon
  • 3,311
  • 23
  • 20
0

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

Community
  • 1
  • 1
Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29