コード例 #1
0
            /// <summary>
            /// returns whether if device is connecte to internet
            /// </summary>
            /// <returns>
            /// true if device is connected to internet,
            /// else false
            /// </returns>
            public static bool IsConnectedToInternet()
            {
                bool connectedToInternet;

                try
                {
                    // init a connection and send request to a stable web host
                    using (TimeoutWebClient timeoutWebClient = new TimeoutWebClient(CONNECTION_TIMEOUT_MILLIS))
                        using (timeoutWebClient.OpenRead(STABLE_WEB_HOST_URL))
                        {
                            // connection succeeded
                            connectedToInternet = true;
                        }
                }
                catch // connection failed
                {
                    connectedToInternet = false;
                }

                return(connectedToInternet);
            }