首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从RedirectStandardOutput捕获CMD输出

无法从RedirectStandardOutput捕获CMD输出
EN

Stack Overflow用户
提问于 2014-01-30 17:21:57
回答 1查看 609关注 0票数 0

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

我见过几个不同的例子,由于示例之间的歧义,或者因为类调用与我的示例不匹配,所以无法实现它们。

基本上,我试图让代码在命令提示符中运行一个命令,然后在一个富文本框中显示输出。到目前为止,我尝试过一次StreamReader尝试,但没有结果。运行此程序时,我会得到错误StandardOut has not been redirected or the process hasn't started yet.

代码语言:javascript
复制
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();
        }

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-30 17:58:23

我通过简单的使用

代码语言:javascript
复制
        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;
            }
        }

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21463233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档