1

我有一个按钮,单击该按钮时正在运行 CMD 提示:

    private void PingCommand_Click(object sender, EventArgs e)
    {
        string strCmdText;
        strCmdText = "";
        System.Diagnostics.Process.Start("CMD.exe", strCmdText); 
    } 

我希望在打开命令提示符之前出现一个框,我可以在其中指定要 ping 的主机名/IP - 这可能吗?如果可以,我该怎么做?

或者,如果可能的话,为多个按钮设置主机名/IP 地址;IE。ping,映射家庭驱动器等!

谢谢

4

5 回答 5

2

虽然可以启动命令行工具,但我建议使用Ping class.Net Framework 4.0(Ping 类)中的可用工具

这是对 MSDN 示例的修改

    using System.Net.NetworkInformation;
    using System.Diagnostics; // for demo purposes only

    private void PingCommand_Click(object sender, EventArgs e)
    {
        Ping pingSender = new Ping ();
        PingOptions options = new PingOptions ();

        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;

        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes (data);
        int timeout = 120;
        string ipOrHost = "80.108.20.100"; // or access your textbox
        //string ipOrHost = txtIP.Text;
        PingReply reply = pingSender.Send (ipOrHost, timeout, buffer, options);
        if (reply.Status == IPStatus.Success)
        {
            Debug.WriteLine ("Address: " + reply.Address.ToString ());
            Debug.WriteLine ("RoundTrip time: " + reply.RoundtripTime);
            Debug.WriteLine ("Time to live: " + reply.Options.Ttl);
            Debug.WriteLine ("Don't fragment: " + reply.Options.DontFragment);
            Debug.WriteLine ("Buffer size: " + reply.Buffer.Length);
        }
    }

如果您确实需要启动任何命令行工具,您可以查看ProcessProcessInfo类(MSDN上的示例代码)

于 2012-08-14T20:20:08.993 回答
0

是的,当然有可能。您应该使用一个对话框窗口提示用户,他们可以在其中输入 IP 地址,然后Process.Start()使用ProcessStartInfo类实例将此 IP 地址传递给方法,该类实例指定要传递给进程的命令行参数。

于 2012-08-14T20:13:57.427 回答
0

您基本上只需要从文本框中获取 IP 然后执行strCmdText = "ping " + MyTextBox.Text;

于 2012-08-14T20:16:09.633 回答
0

您的视图(*.aspx) 类似于

...
<form id="form1" runat="server">
    <div>
        Hostname/IP to ping: <asp:TextBox ID="destination" runat="server" />
        <asp:Button ID="ping" runat="server" Text="Ping it!" onclick="ping_Click" />
    </div>
</form>
...

和您的代码隐藏(*.aspx.cs)

    ...
    protected void ping_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.destination.Text))
        {
            string strCmdText = string.Concat("ping ", this.destination.Text.Trim());
            System.Diagnostics.Process.Start("CMD.exe", strCmdText);
            //and so on...
        }
    }
    ...
于 2012-08-14T20:20:23.580 回答
-1

您可以使用以下代码,它将运行 ping 命令并将结果返回给结果变量。您需要更改 ping.exe 的路径。

var fileName = "C:\\path-to\\ping.exe";
var arguments = txtHostname.Text;

var processStartInfo = new ProcessStartInfo(fileName ,  arguments);
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
using (var process = Process.Start(processStartInfo)) {
    using (var streamReader = new StreamReader(process.StandardOutput.BaseStream, Encoding.UTF8)){
        while (!streamReader.EndOfStream) 
        {
            listBox1.Items.Add(streamReader.ReadLine());
        }
    }
}
于 2012-08-14T20:16:51.347 回答