winform窗体中动态执行任何dos命令问题?求解!急!急!急!
下面是我在网上找的一段代码,我是想在winform中通过按钮来执行文本框中所输入的任何dos命令,并返回执行后的结果,显示
在richText控件中,可是当我输入arp-s、arp-m等命令时为什么没有返回执行结果呢?控件中也没有显示,而像ipconfig、
ipconfig /all等命令时却可以显示返回值,这是为什么呢?
不是很懂了,望各位高手看看,是什么地方出问题了?这段代码有问题吗?还是什么原因呢?急 急 急
/// <summary>
/// 执行单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExecute_Click(object sender, EventArgs e)
{
StringBuilder outputStr = new StringBuilder();
StringBuilder strError = new StringBuilder();
string line = ""; //输出字符串
Process process = new Process(); //创建进程对象
if (txtdosvalue.Text != null && txtdosvalue.Text != "")
{
try
{
process.StartInfo.FileName = "cmd.exe"; //设定需要执行的命令
process.StartInfo.Arguments = "/C " + txtdosvalue.Text; //设定参数,其中的“/C”表示执行完命令后马上退出
process.StartInfo.UseShellExecute = false; //不使用系统外壳程序启动
process.StartInfo.RedirectStandardInput = true; //重定向输入
process.StartInfo.RedirectStandardOutput = true; //重定向输出
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true; //不创建窗口
process.Start();//启动
if (process.HasExited)
{
MessageBox.Show("对不起,暂时无法查找您要的信息", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
richTextValue.Text = "当前命令:" + txtdosvalue.Text;
}
else
{
while (!process.HasExited)
{
outputStr.Append(process.StandardOutput.ReadLine() + "\r\n");
}
richTextValue.Text = outputStr.ToString();//截取输出流显示在控件中
}
process.WaitForExit();//等待程序执行完退出进程
process.Close();//关闭进程
}
catch (Exception)
{
strError.Append(process.StandardError.ReadToEnd() + "\r\n");
richTextValue.Text = strError.ToString();
}
}
else
{
MessageBox.Show("请输入命令", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} --------------------编程问答-------------------- 不错,学习 --------------------编程问答-------------------- 望各位前辈看看,是不是这段代码有问题???求解
补充:.NET技术 , C#