using System.Net.NetworkInformation to check network status
Having spent much of the past 2 years in Sharepoint and SQL Server it was nice to get into .NET / C# and produce a simple app to address a simple problem
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.NetworkInformation;//using System.Runtime.InteropServices;
namespace Beacon{ public partial class NetworkCheck : Form { //[DllImport("kernel32")] //private static extern int Beep(int dwFreq, int dwDuration); public NetworkCheck() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {
} static bool online = false; public static void Status(string sOutput) { SetupNetworkChange();
sOutput += “Network is: “; if (online) sOutput += “online”; else sOutput += “offline”; } static void OnNetworkChange(object sender, EventArgs e) { if (NetworkInterface.GetIsNetworkAvailable()) { if (!online) { online = true; } } else { if (online) { online = false; } } } private static void SetupNetworkChange() { if (NetworkInterface.GetIsNetworkAvailable()) { online = true; } else { online = false; } NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(OnNetworkChange); }
private void timer1_Tick(object sender, EventArgs e) { OnNetworkChange(sender, e); if (online == true) { lbl_Network.Text = “Network is fine”; lbl_Network.ForeColor = System.Drawing.Color.Green; this.WindowState = FormWindowState.Minimized; this.lbl_Subtest.Visible = false; this.Visible = false; } else { lbl_Network.Text = “Your network is down”; lbl_Network.ForeColor = System.Drawing.Color.Red; this.WindowState = FormWindowState.Normal; this.lbl_Subtest.Visible = true; this.TopMost = true; this.Focus(); this.BringToFront(); this.Visible = true; //// Beep(300, 500); } } }}