引用程序集:System.ServiceModel.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace WinINet
{
    class Program
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
        static void Main(string[] args)
        {
            int I = 0;
            bool state = InternetGetConnectedState(out I, 0);
        }
    }
}
添加命名空间using System.Runtime.InteropServices;
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int i = 0;
            if (InternetGetConnectedState(out i, 0))
            {
                MessageBox.Show("联网了!");
            }
            else
            {
                MessageBox.Show("没有联网,无法显示更新数据!");
            }
        }
        [DllImport("wininet")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
    }