'This component raises an event when a network connection is connected/disconnected.
'To try it out, run the sample project and then unplug your network cable...
'... and then plug it back in. An event should be raised for both the disconnect and the reconnect.'By Duncan Mackenzie (Duncanma) ... Check out my GDN profile at
'http://www.gotdotnet.com/community/user/viewprofile.aspx?userid=00011A674C38C375
Imports System.Management<System.ComponentModel.DefaultEvent("NetworkDisconnect")> _
Public Class NetConnectDisconnect
Inherits System.ComponentModel.Component
Private WithEvents WMIDisconnect As ManagementEventWatcher
Private WithEvents WMIConnect As ManagementEventWatcher Public Event NetworkDisconnect(ByVal sender As Object, ByVal e As NetworkEventArgs)
Public Event NetworkConnect(ByVal sender As Object, ByVal e As NetworkEventArgs) Private Sub SetUpWMI()
WMIDisconnect = _
New ManagementEventWatcher("SELECT * FROM MSNdis_StatusMediaDisconnect")
WMIConnect = _
New ManagementEventWatcher("SELECT * FROM MSNdis_StatusMediaConnect") WMIDisconnect.Scope = _
New ManagementScope("root\WMI")
WMIConnect.Scope = _
New ManagementScope("root\WMI") WMIDisconnect.Start()
WMIConnect.Start()
End Sub Private Sub WMIDisconnect_EventArrived(ByVal sender As Object, _
ByVal e As EventArrivedEventArgs) _
Handles WMIDisconnect.EventArrived
'Network Disconnected
Dim instanceName As String
instanceName = CStr(e.NewEvent.Properties("InstanceName").Value)
RaiseEvent NetworkDisconnect(Me, New NetworkEventArgs(instanceName))
End Sub Private Sub WMIConnect_EventArrived(ByVal sender As Object, _
ByVal e As EventArrivedEventArgs) _
Handles WMIConnect.EventArrived
'Network Connected
Dim instanceName As String
instanceName = CStr(e.NewEvent.Properties("InstanceName").Value)
RaiseEvent NetworkConnect(Me, New NetworkEventArgs(instanceName))
End Sub Public Sub New()
SetUpWMI()
End Sub
End ClassPublic Class NetworkEventArgs
Private m_InstanceName As String
Friend Sub New(ByVal instanceName As String)
m_InstanceName = instanceName
End Sub
Public ReadOnly Property InstanceName() As String
Get
Return m_InstanceName
End Get
End Property
End Class
你可以查阅上面的链接的资料
'To try it out, run the sample project and then unplug your network cable...
'... and then plug it back in. An event should be raised for both the disconnect and the reconnect.'By Duncan Mackenzie (Duncanma) ... Check out my GDN profile at
'http://www.gotdotnet.com/community/user/viewprofile.aspx?userid=00011A674C38C375
Imports System.Management<System.ComponentModel.DefaultEvent("NetworkDisconnect")> _
Public Class NetConnectDisconnect
Inherits System.ComponentModel.Component
Private WithEvents WMIDisconnect As ManagementEventWatcher
Private WithEvents WMIConnect As ManagementEventWatcher Public Event NetworkDisconnect(ByVal sender As Object, ByVal e As NetworkEventArgs)
Public Event NetworkConnect(ByVal sender As Object, ByVal e As NetworkEventArgs) Private Sub SetUpWMI()
WMIDisconnect = _
New ManagementEventWatcher("SELECT * FROM MSNdis_StatusMediaDisconnect")
WMIConnect = _
New ManagementEventWatcher("SELECT * FROM MSNdis_StatusMediaConnect") WMIDisconnect.Scope = _
New ManagementScope("root\WMI")
WMIConnect.Scope = _
New ManagementScope("root\WMI") WMIDisconnect.Start()
WMIConnect.Start()
End Sub Private Sub WMIDisconnect_EventArrived(ByVal sender As Object, _
ByVal e As EventArrivedEventArgs) _
Handles WMIDisconnect.EventArrived
'Network Disconnected
Dim instanceName As String
instanceName = CStr(e.NewEvent.Properties("InstanceName").Value)
RaiseEvent NetworkDisconnect(Me, New NetworkEventArgs(instanceName))
End Sub Private Sub WMIConnect_EventArrived(ByVal sender As Object, _
ByVal e As EventArrivedEventArgs) _
Handles WMIConnect.EventArrived
'Network Connected
Dim instanceName As String
instanceName = CStr(e.NewEvent.Properties("InstanceName").Value)
RaiseEvent NetworkConnect(Me, New NetworkEventArgs(instanceName))
End Sub Public Sub New()
SetUpWMI()
End Sub
End ClassPublic Class NetworkEventArgs
Private m_InstanceName As String
Friend Sub New(ByVal instanceName As String)
m_InstanceName = instanceName
End Sub
Public ReadOnly Property InstanceName() As String
Get
Return m_InstanceName
End Get
End Property
End Class
你可以查阅上面的链接的资料
using System ;
using System.Runtime ;
using System.Runtime.InteropServices ; public class InternetCS
{//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( int out Description, int ReservedValue ) ;//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;}}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime ;
using System.Runtime.InteropServices ; namespace 连接互连网
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} //Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(184, 32);
this.button1.TabIndex = 0;
this.button1.Text = "测试互连网连接情况";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 32);
this.button2.TabIndex = 1;
this.button2.Text = "测试接口";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
if ( IsConnectedToInternet() )
{
MessageBox.Show( "可以连接" );
}
else
{
MessageBox.Show( "连接失败" );
}
} //Creating a function that uses the API function...
public bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ; } private void button2_Click(object sender, System.EventArgs e)
{
}
}
}
http://www.c-sharpcorner.com/Code/2003/Aug/CheckInternetConnection.asp
Process.Start("ping","202.106.0.20>netconn.txt");然后分析netconn.txt文件
只要202.106.0.20正常,应该不会有太大问题!