【发布时间】:2014-08-30 05:15:46
【问题描述】:
我想发送带有参数的命令并从 cmd 读取它们的答案。所以,我写了下面的代码,但它不起作用并锁定在屏幕上(myString 通常为空 - “”)。我只想将命令发送到打开的命令提示符。问题出在哪里?提前致谢。 (例如:如何获取 ping 请求的结果?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace CallBatchFile
{
class Program
{
[STAThread]
static void Main()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c date";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string myString = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
}
}
【问题讨论】:
标签: c# process cmd command-line-arguments command-prompt