如果我的 Compact Framework 应用程序有互联网连接,我需要检查它。
所以环顾四周,我找到了这个InternetGetConnectedState
方法,但就我而言,每次我检查我是否有互联网连接时,它都会返回 true,即使我处于离线状态。
这是代码:
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved);
[Flags]enum InternetConnectionState_e : int
{
INTERNET_CONNECTION_MODEM = 0x01,
INTERNET_CONNECTION_LAN = 0x02,
INTERNET_CONNECTION_PROXY = 0x04,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}
public Form1()
{
InitializeComponent();
verify();
}
private void verify()
{
// In function for checking internet
InternetConnectionState_e flags = 0;
bool isConnected = InternetGetConnectedState(ref flags, 0);
textBox1.Text = "Con: " + isConnected.ToString();
textBox1.Text += "\r\nDescr: " + flags.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
verify();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
我做错了什么?