【问题标题】:Unable to capture CMD output from RedirectStandardOutput无法从 RedirectStandardOutput 捕获 CMD 输出
【发布时间】:2014-01-31 01:58:23
【问题描述】:

我正在尝试在命令中运行具有不同值的命令(基于用户在运行时在表单文本框中键入的内容。

我看过一些不同的示例,但由于示例之间的歧义或类调用与我的示例不匹配而无法实现它们。

基本上,我试图让代码在命令提示符下运行命令,然后在富文本框中显示输出。到目前为止,我已经尝试过 StreamReader 尝试,但无济于事。运行此程序时出现错误StandardOut has not been redirected or the process hasn't started yet.

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();
        }

实现我的请求的正确方法是什么?谢谢。

【问题讨论】:

标签: c# .net


【解决方案1】:

我通过简单地使用它来工作

        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;
            }
        }

我遇到的所有其他例子都太复杂了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2017-06-04
    • 2013-01-16
    • 1970-01-01
    • 2019-02-23
    • 2014-07-26
    相关资源
    最近更新 更多