C#相当于“My.Computer.Network.Ping”

时间:2009-11-25 14:45:11

标签: c# .net

问题说真的,VB.NET的My.Computer.Network.Ping是否有C#答案?

干杯!

5 个答案:

答案 0 :(得分:6)

请参阅http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

    // args[0] can be an IPaddress or host name.
    public static void Main (string[] args)
    {
        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;
        PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
        if (reply.Status == IPStatus.Success)
        {
            Console.WriteLine ("Address: {0}", reply.Address.ToString ());
            Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
            Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
            Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
            Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
        }
    }

答案 1 :(得分:3)

为什么不直接添加对Microsoft.VisualBasic.dll的引用?在解决方案资源管理器中,右键单击解决方案,然后选择添加参考。出现“引用”对话框时,找到并选择Microsoft.VisualBasic.dll,单击确定

Microsoft.VisualBasic.Devices.Network net = new Network();
bool success = net.Ping("www.google.com");

答案 2 :(得分:3)

public bool Ping(host)
{
    System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
    if (p.Send(host, 500).Status == System.Net.NetworkInformation.IPStatus.Success) {
        return true;
    } else {
        return false;
    }
}

答案 3 :(得分:2)

System.Net.NetworkInformation 命名空间中有 Ping 类。

答案 4 :(得分:1)

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

编辑:或者,您可以添加对{GAC& G}中的Microsoft.VisualBasic.dll的引用。从c#中使用它,而不是自己编写代码;)