0

I am trying to run a command with different values in the command(based on what the user types into the form text boxes during runtime.

I've seen a few different examples and have not been able to implement them either due to ambiguities amongst examples or because the class calls aren't matching my example.

Basically, I'm trying to get the code to run a command in the command prompt and then display the output in a rich text box. I've attempted an StreamReader attempt so far, to no avail. I get the error StandardOut has not been redirected or the process hasn't started yet. when running this.

Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        string contentType = "\"Content-Type:text/xml\"";
        string output = string.Empty;
        string command = @"c:\curl -s -k -H "+contentType+" -u "+txtBoxIntgrName.Text+":"+txtBoxIntgrPass.Text+" -g https://"+txtBoxDomain.Text+"/wabapps/bb-data-integrat-flatfile-BBLEARN/endpoint/dataSetStatus/"+txtBoxReferenceID.Text;
        startInfo.Arguments = "/user:Administrator \"cmd /c " + command + "\"";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo = startInfo;

        process.Start();
        using (StreamReader streamReader = process.StandardOutput)
        {
            rchTxtBoxDataSetStatus.Text = streamReader.ReadToEnd();
        }

What is the correct way to implement my request? Thank you.

Christopher Bruce
  • 661
  • 3
  • 10
  • 24

1 Answers1

1

I got it working by simply using

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        string contentType = "\"Content-Type:text/xml\"";

        string command = @"C:\blackboard\apps\sis-controller\sis-data\curl -s -k -H " + contentType + " -u " + txtBoxIntgrName.Text + ":" + txtBoxIntgrPass.Text + " -g https://" + txtBoxDomain.Text + "/webapps/bb-data-integration-flatfile-BBLEARN/endpoint/dataSetStatus/" + txtBoxReferenceID.Text;
        startInfo.Arguments = "/user:Administrator \"cmd /c " + command + "\"";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        using (Process process = Process.Start(startInfo))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                rchTxtBoxDataSetStatus.Text = result;
            }
        }

All the other examples I've come across were too needlessly complicated.

Christopher Bruce
  • 661
  • 3
  • 10
  • 24