IntroductionI need to monitor Network status in my project.To check the cable has bad contact or not and to check network is enable or not.So I develop a simple tool to test.
BackgroundThis sample is using WMI to get information in .NET.
Using the Code I use a singleton to manager all network information. Now, call this code to initialize and monitor:NetworkManager.Instance.StartMonitor();And foreach NetworkManager.Instance.Informations.Values to get information: foreach(NetworkInfo info in NetworkManager.Instance.Informations.Values)
{
Console.WriteLine("Device Name:" + info.DeviceName);
Console.WriteLine("Adapter Type:" + info.AdapterType);
Console.WriteLine("MAC Address:" + info.MacAddress);
Console.WriteLine("Connection Name:" + info.ConnectionID);
Console.WriteLine("IP Address:" + info.IP);
Console.WriteLine("Connection Status:" + info.Status.ToString());
}At last call this to destory: NetworkManager.Instance.Destory();Abort the Codepublic enum NetConnectionStatus
{
Disconnected = 0,
Connecting = 1,
Connected = 2,
Disconnecting = 3,
HardwareNotPresent = 4,
HardwareDisabled = 5,
HardwareMalfunction = 6,
MediaDisconnected = 7,
Authenticating = 8,
AuthenticationSucceeded = 9,
AuthenticationFailed = 10,
InvalidAddress = 11,
CredentialsRequired = 12
} First, this enum named NetConnectionStatus that is reference from the property NetConnectionStatus in Win32NetworkAdapter class.
Read more: Codeproject
QR:
BackgroundThis sample is using WMI to get information in .NET.
Using the Code I use a singleton to manager all network information. Now, call this code to initialize and monitor:NetworkManager.Instance.StartMonitor();And foreach NetworkManager.Instance.Informations.Values to get information: foreach(NetworkInfo info in NetworkManager.Instance.Informations.Values)
{
Console.WriteLine("Device Name:" + info.DeviceName);
Console.WriteLine("Adapter Type:" + info.AdapterType);
Console.WriteLine("MAC Address:" + info.MacAddress);
Console.WriteLine("Connection Name:" + info.ConnectionID);
Console.WriteLine("IP Address:" + info.IP);
Console.WriteLine("Connection Status:" + info.Status.ToString());
}At last call this to destory: NetworkManager.Instance.Destory();Abort the Codepublic enum NetConnectionStatus
{
Disconnected = 0,
Connecting = 1,
Connected = 2,
Disconnecting = 3,
HardwareNotPresent = 4,
HardwareDisabled = 5,
HardwareMalfunction = 6,
MediaDisconnected = 7,
Authenticating = 8,
AuthenticationSucceeded = 9,
AuthenticationFailed = 10,
InvalidAddress = 11,
CredentialsRequired = 12
} First, this enum named NetConnectionStatus that is reference from the property NetConnectionStatus in Win32NetworkAdapter class.
Read more: Codeproject
QR: