我试图在命令中运行一个具有不同值的命令(基于运行时用户在表单文本框中键入的内容)。
我见过几个不同的例子,由于示例之间的歧义,或者因为类调用与我的示例不匹配,所以无法实现它们。
基本上,我试图让代码在命令提示符中运行一个命令,然后在一个富文本框中显示输出。到目前为止,我尝试过一次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();
}实现我的请求的正确方法是什么?谢谢。
发布于 2014-01-30 17:58:23
我通过简单的使用
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;
}
}我遇到的其他例子都太复杂了。
https://stackoverflow.com/questions/21463233
复制相似问题