首页 > 代码库 > c# 判断网络是连接到互联网

c# 判断网络是连接到互联网



方法1:InternetGetConnectedState

        [System.Runtime.InteropServices.DllImport("wininet")]
        private extern static bool InternetGetConnectedState(out int connectionDescriptionint reservedValue);
       
        private void button1_Click(object senderEventArgs e)
        {
            int i = 0;
            if (InternetGetConnectedState(out i, 0))
                MessageBox.Show("联网状态");
            else
                MessageBox.Show("你网线掉了啦");
        }
http://www.cnblogs.com/hihell/archive/2011/09/20/2182626.html

修改下 
       [System.Runtime.InteropServices.DllImport("wininet")]
        private extern static bool InternetGetConnectedState(out int connectionDescriptionint reservedValue);
        //判断网络是连接到互联网
        public static bool IsNetWorkConnect()
        {
            int i = 0;
          return InternetGetConnectedState(out i, 0) ? true:false;
        }

          
            if ( IsNetWorkConnect() )
                MessageBox.Show("已连接到互联网");
            else
                MessageBox.Show("无网络")







来自为知笔记(Wiz)