2016-12-06 5 views
-3

私は私のマシンは、他のネットワーク・エンティティでpingを実行できるかどうかをテストしようとしている:マシンがC#でpingできるかどうかを知る方法?

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
string allowed = mgr.LocalPolicy.CurrentProfile.IcmpSettings.AllowInboundEchoRequest; 
+6

。 110%29.aspx?f = 255&MSPPError = -2147217396)class? –

+0

@WaiHaLeeそれは答えなので、これはans答えを投稿するかもしれません? ;) – nozzleman

答えて

0

マシンがpingを受けることができるかどうかを知るために。 ICMPタイプ8 inとタイプ0 outを許可する必要があります。 [ `System.Net.Networkinformation.Ping`(https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vsを使用しない理由

public Boolean IsPingable() 
    { 
     Boolean icmpAllowed = false; 
     INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
     Object allowedin8 = null; 
     Object restrictedin8 = null; 
     Object allowedout0 = null; 
     Object restrictedout0 = null; 
     mgr.IsIcmpTypeAllowed(NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4,"127.0.0.1", 0, out allowedin0, out restrictedin0); 
     mgr.IsIcmpTypeAllowed(NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4, "8.8.8.8", 8, out allowedout8, out restrictedout8); 
     if ((Boolean)allowedin0 && (Boolean)allowedout8) 
     { 
      icmpAllowed = true; 
     } 
     return icmpAllowed; 
    } 
0

あなたはそれがpingを実行することが可能かどうかを確認するには、pingを送信することができます

public static bool CanBePinged(string hostname) 
{ 
    if (string.IsNullOrWhiteSpace(hostname)) throw new ArgumentException("hostname"); 

    Ping p1 = new Ping(); 
    try 
    { 
     PingReply PR = p1.Send(hostname); 
     return PR.Status == IPStatus.Success; 
    }catch(Exception e) 
    { 
     return false; 
    } 
} 
0

使用System.Net.Networkinformation.Ping.Send(...)を。

あなたはthe MSDN sampleがあり、その場合には、ホスト名を指定するなど、いくつかの方法、でPingを使用することができます。

public static void SimplePing() 
{ 
    Ping pingSender = new Ping(); 
    PingReply reply = pingSender.Send ("www.contoso.com"); 

    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); 
    } 
    else 
    { 
     Console.WriteLine (reply.Status); 
    } 
} 

か、the MSDN sampleがある場合にはIPAddress、指定することができます。

public static void LocalPing() 
{ 
    // Ping's the local machine. 
    Ping pingSender = new Ping(); 
    IPAddress address = IPAddress.Loopback; 
    PingReply reply = pingSender.Send (address); 

    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); 
    } 
    else 
    { 
     Console.WriteLine (reply.Status); 
    } 
} 
+0

私はリモートマシンにpingできるかどうかを確認しようとしていませんが、マシンにpingを実行できるかどうかを確認しようとしていますか? –

+0

最初のサンプルでは、​​ "www.contoso.com"を 'localhost'に変更し、' '存続時間' 'と' 'Do not fragment''行を削除できます(' NullReferenceException'をスローします) )、マシンがpingに応答するかどうかを知らせます。 2番目のサンプルは、** [** IPAddress.Loopback'](https://msdn.microsoft.com/en-us/library/system.net.ipaddress.loopback)を使用しています。機械。 –

+0

マシンにpingを実行できない場合、成功ステータスを戻します。 –

関連する問題