在C#中运行命令之前设置主机名/ IP

时间:2012-08-14 20:09:13

标签: c#

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

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

我想要发生的是在打开命令提示符之前我想要一个框出现在我可以指定要ping的主机名/ IP的位置 - 这是可能的,如果可以的话,我将如何这样做?

如果可能,或者为多个按钮设置主机名/ IP地址;即。 ping,map home drive等等!

由于

5 个答案:

答案 0 :(得分:2)

虽然可以启动命令行工具,但我建议使用.Net Framework 4.0中提供的Ping classPing Class

这是对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);
        }
    }

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

答案 1 :(得分:0)

是的,当然有可能。您应该使用一个对话框窗口提示用户输入IP地址,然后使用ProcessStartInfo类实例将此IP地址传递给Process.Start()方法,该实例指定要传递给的command-line arguments这个过程。

答案 2 :(得分:0)

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

答案 3 :(得分: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...
        }
    }
    ...

答案 4 :(得分:-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());
        }
    }
}