in Eski Blog Yazılarım

Örnek Kod: C# wininet ile bağlantı kontrolü

C# kullanarak windows’un internete bağlı olup olmadığını aşağıdaki kod ile öğrenebilirsiniz. Özellikle programınız internete bağlanıp işlem yapıcaksa güzel bir kontrol.

    public class Baglanti
    {
        [DllImport("WININET", CharSet = CharSet.Auto)]
        public static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);

        public enum InternetConnectionState : int
        {                                              
            INTERNET_CONNECTION_OFFLINE = 0x20,
            INTERNET_CONNECTION_CONFIGURED = 0x40
        }

        static void Main(string[] args)
        {
            InternetConnectionState flags = 0;
            bool isConfigured = (flags & InternetConnectionState.INTERNET_CONNECTION_CONFIGURED) != 0;
            bool isConnected = InternetGetConnectedState(ref flags, 0);
            //sonuç
            if (isConnected == true)
            {
                Console.WriteLine("İnternete Bağlısınız");
            }
            else
            {
                Console.WriteLine("İnternet YOK!");
            }
        }
    }

Yorum Bırak

Comment