1

クリックするとCMDプロンプトが実行されるボタンがあります。

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

コマンド プロンプトを開く前に、ping を実行するホスト名/IP を指定できるボックスを表示したいのですが、これは可能ですか。

または可能であれば、ホスト名/IP アドレスを複数のボタンに設定します。すなわち。ping、ホームドライブのマップなど!

ありがとう

4

5 に答える 5

2

コマンドライン ツールを起動することは可能ですがPing class、.Net Framework 4.0 ( Ping 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);
        }
    }

本当にコマンドライン ツールを起動する必要がある場合はProcess、クラス ( MSDNProcessInfoのサンプル コード)を参照してください。

于 2012-08-14T20:20:08.993 に答える
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 に答える
0

はい、もちろん可能です。ユーザーが IP アドレスを入力できるダイアログ ウィンドウを表示し、プロセスに渡すコマンド ライン引数を指定するProcessStartInfoクラス インスタンスをProcess.Start()使用して、この IP アドレスをメソッドに渡す必要があります。

于 2012-08-14T20:13:57.427 に答える
0

基本的には、テキスト ボックスから IP を取得するだけで済みます。strCmdText = "ping " + MyTextBox.Text;

于 2012-08-14T20:16:09.633 に答える
-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 に答える