Search code examples
c#network-programmingcompact-frameworkopennetcf

failed try: OpenNETCF Send Method in Ping class


I have tried to ping with OpenNETCF like Mr."CNLSH" in this reference and it works fine if the host is available. If not, pingReply.Status never becomes false because i'll get an exception from send-Method (could not send package). Is there a possibility to work without try/catch?

    public static bool pingIP(String s_ip)
    {
        Ping ping = new Ping();
        PingReply pingReply;
        try
        {
            pingReply = ping.Send(s_ip,500);
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.WriteLine("could not connect to " + s_ip);
            return false;
        }
        if (pingReply.Status == IPStatus.Success)
            return true;
        else return false;
    }

Solution

  • No. You need to catch the exceptions. Also, the Ping class is disposable, so you should dispose of it when you're finished.

    using(Ping ping = new Ping())
    {
        /* ... */
    }
    

    see: http://www.opennetcf.com/library/sdf/html/7555257b-b797-37d3-ff61-4e90f731be99.htm