C#檢測當前網絡連接狀態

 1. 方法定義

[DllImport("wininet.dll")]

private extern static bool InternetGetConnectedState( out int connectionDescription, int reservedValue ) ;

2. 方法說明

參數:

connectionDescription : 連接說明

reservedValue : 保留值

返回值:

true: On Line

false: Off Line

3. 調用方法

a. 你必須在你的code裏引用System.Runtime.InteropServices,否則,會有編譯錯誤

b. 定義一個變量 int I = 0;

c. 調用bool state = InternetGetConnectedState(out I,0);



完整的代碼:

using System.Runtime.InteropServices;



namespace internet

{

public class Class1

{

[DllImport("wininet.dll")]

private extern static bool InternetGetConnectedState( out int connectionDescription, int reservedValue ) ;

public Class1(){}

private bool IsConnected()

{

int I=0;

bool state = InternetGetConnectedState(out I,0);

return state;

}

}

}
發佈了31 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章