【发布时间】:2014-08-23 11:41:23
【问题描述】:
在学习 C# 的过程中,我创建了一个基于 winforms 的应用程序,通过单击 button 来 ping google.com。所以,当我点击button 时,command prompt 出现,但ping 命令没有运行,最终command prompt 窗口在一段时间后出现。
怎么了 ?为什么ping 没有执行?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/C ping www.google.com";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
}
}
【问题讨论】:
-
你的意思是收到2或3个回复后退出?
-
我正在得到任何东西。命令提示符启动,保持空白 20 秒然后关闭。
-
在域之后添加 `-t'。 IE。 @"/C ping www.google.com -t" 然后再试一次
-
你应该对 output 变量做一些有用的事情。
-
@Shell : 而且,现在命令提示符永远保留在那里 :(
标签: c# winforms shell command-line command-prompt