using System.Net.NetworkInformation to check network status

No comments September 20th, 2011

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);
}
}
}
}

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);            }        }    }}

Display and RSS feed using HTML and JScript

No comments August 20th, 2011

Here’s the code you need in Red.  This example displays a feed from www.ozcruiseblog.com/?feed=rss (?feed=rss is the standard wordpess feed surfix).

It uses code from a 3rd party and you can build your’s here http://feed2js.org/index.php?s=build

<script language=”JavaScript” src=”http://feed2js.org//feed2js.php?src=http%3A%2F%2Fwww.alexcrossley.net%2F%3Ffeed%3Drss2&chan=y&desc=1&utf=y”  charset=”UTF-8″ type=”text/javascript”></script>
<noscript>
<a href=”http://feed2js.org//feed2js.php?src=http%3A%2F%2Fwww.alexcrossley.net%2F%3Ffeed%3Drss2&chan=y&desc=1&utf=y&html=y”>View RSS feed</a>
</noscript>

<script language=”JavaScript” src=”http://feed2js.org//feed2js.php?src=http%3A%2F%2Fwww.alexcrossley.net%2F%3Ffeed%3Drss2&chan=y&desc=1&utf=y”  charset=”UTF-8″ type=”text/javascript”></script>
<noscript><a href=”http://feed2js.org//feed2js.php?src=http%3A%2F%2Fwww.alexcrossley.net%2F%3Ffeed%3Drss2&chan=y&desc=1&utf=y&html=y”>View RSS feed</a></noscript>