Network Availability detection

F

Folkien

Hello everybody.

I want to make a little programme to show the network availability
status. I read that I had to use
Microsoft.VisualBasic.Devices, creat a delegate to manage the event.

Here is the code that I made but of course it dosn't work

namespace WindowsApplication2
{
public delegate void MyApplication_NetworkAvailabilityChanged(object
sender, Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs e);

partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(92, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(125, 62);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(56, 185);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

//
// Network availability
//
this.netwatch = new NetworkWatching();
this.netwatch.AvChange += new
MyApplication_NetworkAvailabilityChanged(this.netwat_AvChange);

}

#endregion

private System.Windows.Forms.Button button1;
private NetworkWatching netwatch;
private System.Windows.Forms.Label label1;
}

class NetworkWatching
{
public event MyApplication_NetworkAvailabilityChanged AvChange;
}
}

this is in the Form1.Designer.cs

In the Form1.cs I added :

private void DisplayAvail(string avail)
{
label1.Text = avail;
}

void netwat_AvChange(object sender,
Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs e)
{
this.DisplayAvail(e.IsNetworkAvailable.ToString());
}

Any idea what i have to do to make what i want ? I now that probably I
miss a conceptual clarification.

Anyway, thx a lot for the help
 
F

Folkien

Ok I went an other way, I try now to use the WMI api. I have to find
what is the event linked to network managment to make my query but It
seam's much easier
 
M

Michael Rubinstein

Folkejen, look up System.Net.NetworkInformation.NetworkChange class. It
has two public events - NetworkAvailabilityChanged and
NetworkAddressChanged. C# help for NetworkAddressChanged comes with code
fragment showing how to use this event. The other one can be handled in the
same manner.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top