none
How to detect if local area network has become disable? RRS feed

  • Question

  • Hello,

    I am looking to develop a simple vb.net code that will sense whenever a local area network becomes inactive, and to enable the wireless adapter. Then, it would be nice to have the code verify if all network shares are still available. if not, the code should attempt to initiate reconnecting the users' network shares.

    The goal is allow laptop users to continue having a network connection even if the local area stop working for whatever reason.


    • Edited by PedroRobles Thursday, November 7, 2013 10:33 PM clarify information
    Thursday, November 7, 2013 10:24 PM

Answers

  • Hello,

    I am looking to develop a simple vb.net code that will sense whenever a local area network becomes inactive, and to enable the wireless adapter. Then, it would be nice to have the code verify if all network shares are still available. if not, the code should attempt to initiate reconnecting the users' network shares.

    The goal is allow laptop users to continue having a network connection even if the local area stop working for whatever reason.


    Well this link has a project you can download that will allow you to enable and disable network adapters whether they be WI-FI or 10/100base T NICs or whatever has an IP stack associated to it I suppose. Asl long as the NIC vendor allows WMI to enable and disable their NIC I guess which they probably all do nowadays.

    And the code below should give you the status of your NICs I think. But the status of my 10/100 base T NIC for hardwiring to a network connection (router, whatever) doesn't show up because it is disabled since I don't use it.

    The bottom code works for me to detect if the network is up but when I originally posted this code a few years ago someone I posted it for was running an earlier version of Visual Studio on an older O/S and it would not work for them for some reason. Perhaps they didn't have a .Net rev or maybe it's the O/S high enough that had the NETWORKLIST.NetworkListManager API.

    So I would imagine that with these codes you could splice something together that could detect network shutdown, disable the NIC on that network, enable the NIC for WI-FI, get the status of the WI-FI NIC if needed although I'm not sure that info is necessary. Or don't disable the NIC on that network in case it comes back up so you could then turn off the WI-FI NIC or something. As far as network shares I can't help you there. I have none and have no way to figure out how to re-establish connectivity to them via a different network if this scenario occured. And then change their connectivity back to the other network when it comes up again.

    Imports System.Net.NetworkInformation
    Imports System.Net
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim PrimaryNic As New Collection
            Dim PNic As String = ""
    
            For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
    
                For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses
                    ' if gateway address is NOT 0.0.0.0 and the network card status is UP then we've found the main network card  
                    If gatewayAddr.Address.ToString <> "0.0.0.0" And networkCard.OperationalStatus.ToString() = "Up" Then
                        RichTextBox1.AppendText("Interface GUID: " & networkCard.Id & vbCrLf & "Name: " & networkCard.Id & vbCrLf & "Description: " & networkCard.Description & vbCrLf & "Status: " & networkCard.OperationalStatus.ToString & vbCrLf & "Speed: " & (networkCard.Speed / 1000000).ToString("#,000") & " Mbps" & vbCrLf & "MAC Address: " & networkCard.GetPhysicalAddress.ToString & vbCrLf)
    
                        ' Get IP Address(es) and subnet(s) information  
    
                        Dim IpAddress As UnicastIPAddressInformation
    
                        For Each IpAddress In networkCard.GetIPProperties.UnicastAddresses
                            RichTextBox1.AppendText("IP Address and Subnet: " & IpAddress.Address.ToString & vbCrLf)
                        Next
                        ' Get IP gateway information  
                        RichTextBox1.AppendText("Gateway: " & gatewayAddr.Address.ToString & vbCrLf)
    
                        ' Get IP DNS information  
                        Dim DnsAddress As IPAddress
    
                        For Each DnsAddress In networkCard.GetIPProperties.DnsAddresses
                            RichTextBox1.AppendText("DNS entry: " & DnsAddress.ToString & vbCrLf)
                        Next
    
                        ' Other IP information  
                        Dim IPProp As IPInterfaceProperties = networkCard.GetIPProperties
    
                        If Not IPProp Is Nothing Then
                            RichTextBox1.AppendText("DNS Enabled: " & IPProp.IsDnsEnabled.ToString & vbCrLf)
                            RichTextBox1.AppendText("Dynamic DNS: " & IPProp.IsDynamicDnsEnabled.ToString & vbCrLf)
                        End If
    
                        Dim IPv4 As IPv4InterfaceProperties = networkCard.GetIPProperties.GetIPv4Properties
    
                        If Not IPv4 Is Nothing Then
                            RichTextBox1.AppendText("DHCP Enabled: " & IPv4.IsDhcpEnabled.ToString & vbCrLf)
                            RichTextBox1.AppendText("MTU Setting: " & IPv4.Mtu.ToString & vbCrLf)
                            RichTextBox1.AppendText("Uses WINS: " & IPv4.UsesWins.ToString & vbCrLf & vbCrLf & vbCrLf)
                        End If
    
                    End If
                Next
            Next
    
    
        End Sub
    End Class

    Public Class Form1
    
        'Add reference, COM, Network List Manager 1.0 Type Library
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Interval = 10000
            Timer1.Start()
        End Sub
    
        Private Function IsConnectedToInternet() As Boolean
            If System.Environment.OSVersion.Version.Major >= 6 Then
                'Vista and Up
                Dim Networks As New NETWORKLIST.NetworkListManager
                If Networks.IsConnectedToInternet Then
                    Return True
                Else
                    Return False
                End If
            Else
                MessageBox.Show("Your OS is incompatable")
            End If
            Return Nothing
        End Function
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            RichTextBox1.Text = "Is the network connected = " & CStr(IsConnectedToInternet())
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Friday, November 8, 2013 4:52 AM
  • I used this code to get 40 different properties for each Network Adapter and Network Adapter does not mean a NIC necessarily as the list returned contains a ref to a "Microsoft Teredo Tunneling Adapter" (From Yahoo answers - Teredo tunneling is a transition technology that gives full IPv6 connectivity for IPv6-capable hosts which are on the IPv4 Internet but which have no direct native connection to an IPv6 network) which I would believe has nothing to do with hardware as I think that is software that resides somewhere in the IP layers for providing that capability. Although I haven't studied it enough to really know.

    So the code is below, the returned info for my two wireless adapters below that and the WMI code creators showing what I used below that. Couldn't figure out how to establish an event for if a NIC loses network connectivity though. Also put my standby Linksys WI-FI adapter in aluminum foil, then in a can and finally put a metal cooking pot over the can but it didn't lose WI-FI connectivity which I receive from my renter so it's not like I have a local wi-fi router that I can turn off to lose connectivity to see what is returned.

    Also in the info for my wi-fi adapters note that the Atheros returned an entry for being connect to the LAN where the Linksys did not even though the Linsys is connected to the wi-fi network. But the Atheros is being used for my link to the internet, not the Linksys so I suppose that is why that occured. Therefore it's possible that if you can't get an event handler to be used to detect link loss to a LAN by your 10/100 base t NIC you could use a timer to get this info from it and see if the line "NetConnectionID: Local Area Connection"s next line for "NetConnectionStatus: 4" changes when that NIC loses LAN connectivity. Or if that entry no longer gets returned since a LAN is no longer being connected to.

    Of course there's other possible code that WMI can create for other things related to networking which may provide info that what I picked does not. So you may need to look through a lot of information and test code to see what it does. Fortunately there does not appear to be many refs to networks, etc so you shouldn't have to spend maybe more than a couple hours seeing what different code does and testing it relative to the LAN going away and coming back. Also a LAN can be up even though Internet connectivity is gone or the server on the LAN that provides the shares (which I suppose you mean drives that can be shared from the server or something) is down.

    Imports System
    Imports System.Management
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Text = ""
            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NetworkAdapter")
    
                For Each queryObj As ManagementObject In searcher.Get()
    
                    RichTextBox1.AppendText("-----------------------------------" & vbCrLf)
                    RichTextBox1.AppendText("Win32_NetworkAdapter instance" & vbCrLf)
                    RichTextBox1.AppendText("-----------------------------------" & vbCrLf)
                    RichTextBox1.AppendText("NetConnectionID: " & queryObj("NetConnectionID") & vbCrLf)
                    RichTextBox1.AppendText("NetConnectionStatus: " & queryObj("NetConnectionStatus") & vbCrLf)
                    RichTextBox1.AppendText("NetEnabled: " & queryObj("NetEnabled") & vbCrLf)
    
                    If queryObj("NetworkAddresses") Is Nothing Then
                        RichTextBox1.AppendText("NetworkAddresses: " & queryObj("NetworkAddresses") & vbCrLf)
                    Else
                        Dim arrNetworkAddresses As String()
                        arrNetworkAddresses = CType(queryObj("NetworkAddresses"), String())
                        For Each arrValue As String In arrNetworkAddresses
                            RichTextBox1.AppendText("NetworkAddresses: " & arrValue & vbCrLf)
                        Next
                    End If
                    RichTextBox1.AppendText("PermanentAddress: " & queryObj("PermanentAddress") & vbCrLf)
                    RichTextBox1.AppendText("PhysicalAdapter: " & queryObj("PhysicalAdapter") & vbCrLf)
                    RichTextBox1.AppendText("PNPDeviceID: " & queryObj("PNPDeviceID") & vbCrLf)
    
                    If queryObj("PowerManagementCapabilities") Is Nothing Then
                        RichTextBox1.AppendText("PowerManagementCapabilities: " & queryObj("PowerManagementCapabilities") & vbCrLf)
                    Else
                        Dim arrPowerManagementCapabilities As UInt16()
                        arrPowerManagementCapabilities = CType(queryObj("PowerManagementCapabilities"), UShort())
                        For Each arrValue As UInt16 In arrPowerManagementCapabilities
                            RichTextBox1.AppendText("PowerManagementCapabilities: " & arrValue & vbCrLf)
                        Next
                    End If
                    RichTextBox1.AppendText("PowerManagementSupported: " & queryObj("PowerManagementSupported") & vbCrLf)
                    RichTextBox1.AppendText("ProductName: " & queryObj("ProductName") & vbCrLf)
                    RichTextBox1.AppendText("ServiceName: " & queryObj("ServiceName") & vbCrLf)
                    RichTextBox1.AppendText("Speed: " & queryObj("Speed") & vbCrLf)
                    RichTextBox1.AppendText("Status: " & queryObj("Status") & vbCrLf)
                    RichTextBox1.AppendText("StatusInfo: " & queryObj("StatusInfo") & vbCrLf)
                    RichTextBox1.AppendText("SystemCreationClassName: " & queryObj("SystemCreationClassName") & vbCrLf)
                    RichTextBox1.AppendText("SystemName: " & queryObj("SystemName") & vbCrLf)
                    RichTextBox1.AppendText("TimeOfLastReset: " & queryObj("TimeOfLastReset") & vbCrLf)
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub
    
    End Class

    This information returned for my two wireless adapaters

    Atheros adapters info
    
    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: Local Area Connection
    NetConnectionStatus: 4
    NetEnabled: False
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: True
    PNPDeviceID: PCI\VEN_1969&DEV_1062&SUBSYS_04591025&REV_C0\4&3170ED28&0&00E2
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: Atheros AR8132 PCI-E Fast Ethernet Controller (NDIS 6.20)
    ServiceName: L1C
    Speed: 
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420
    
    
    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: Wireless Network Connection
    NetConnectionStatus: 2
    NetEnabled: True
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: True
    PNPDeviceID: PCI\VEN_168C&DEV_002A&SUBSYS_E01F105B&REV_01\4&21D98CC8&0&00E1
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: [CommView] Atheros AR5B93 Wireless Network Adapter
    ServiceName: TS_AR5416
    Speed: 130000000
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420
    
    
    Linsys adapters info
    
    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: Wireless Network Connection 2
    NetConnectionStatus: 2
    NetEnabled: True
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: True
    PNPDeviceID: USB\VID_1737&PID_0078\5&2888D0FF&0&1
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: Linksys RangePlus Wireless USB Network Adapter
    ServiceName: netr28ux
    Speed: 153250000
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420
    


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Friday, November 8, 2013 1:35 PM

All replies

  • Hello,

    I am looking to develop a simple vb.net code that will sense whenever a local area network becomes inactive, and to enable the wireless adapter. Then, it would be nice to have the code verify if all network shares are still available. if not, the code should attempt to initiate reconnecting the users' network shares.

    The goal is allow laptop users to continue having a network connection even if the local area stop working for whatever reason.


    Well this link has a project you can download that will allow you to enable and disable network adapters whether they be WI-FI or 10/100base T NICs or whatever has an IP stack associated to it I suppose. Asl long as the NIC vendor allows WMI to enable and disable their NIC I guess which they probably all do nowadays.

    And the code below should give you the status of your NICs I think. But the status of my 10/100 base T NIC for hardwiring to a network connection (router, whatever) doesn't show up because it is disabled since I don't use it.

    The bottom code works for me to detect if the network is up but when I originally posted this code a few years ago someone I posted it for was running an earlier version of Visual Studio on an older O/S and it would not work for them for some reason. Perhaps they didn't have a .Net rev or maybe it's the O/S high enough that had the NETWORKLIST.NetworkListManager API.

    So I would imagine that with these codes you could splice something together that could detect network shutdown, disable the NIC on that network, enable the NIC for WI-FI, get the status of the WI-FI NIC if needed although I'm not sure that info is necessary. Or don't disable the NIC on that network in case it comes back up so you could then turn off the WI-FI NIC or something. As far as network shares I can't help you there. I have none and have no way to figure out how to re-establish connectivity to them via a different network if this scenario occured. And then change their connectivity back to the other network when it comes up again.

    Imports System.Net.NetworkInformation
    Imports System.Net
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim PrimaryNic As New Collection
            Dim PNic As String = ""
    
            For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
    
                For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses
                    ' if gateway address is NOT 0.0.0.0 and the network card status is UP then we've found the main network card  
                    If gatewayAddr.Address.ToString <> "0.0.0.0" And networkCard.OperationalStatus.ToString() = "Up" Then
                        RichTextBox1.AppendText("Interface GUID: " & networkCard.Id & vbCrLf & "Name: " & networkCard.Id & vbCrLf & "Description: " & networkCard.Description & vbCrLf & "Status: " & networkCard.OperationalStatus.ToString & vbCrLf & "Speed: " & (networkCard.Speed / 1000000).ToString("#,000") & " Mbps" & vbCrLf & "MAC Address: " & networkCard.GetPhysicalAddress.ToString & vbCrLf)
    
                        ' Get IP Address(es) and subnet(s) information  
    
                        Dim IpAddress As UnicastIPAddressInformation
    
                        For Each IpAddress In networkCard.GetIPProperties.UnicastAddresses
                            RichTextBox1.AppendText("IP Address and Subnet: " & IpAddress.Address.ToString & vbCrLf)
                        Next
                        ' Get IP gateway information  
                        RichTextBox1.AppendText("Gateway: " & gatewayAddr.Address.ToString & vbCrLf)
    
                        ' Get IP DNS information  
                        Dim DnsAddress As IPAddress
    
                        For Each DnsAddress In networkCard.GetIPProperties.DnsAddresses
                            RichTextBox1.AppendText("DNS entry: " & DnsAddress.ToString & vbCrLf)
                        Next
    
                        ' Other IP information  
                        Dim IPProp As IPInterfaceProperties = networkCard.GetIPProperties
    
                        If Not IPProp Is Nothing Then
                            RichTextBox1.AppendText("DNS Enabled: " & IPProp.IsDnsEnabled.ToString & vbCrLf)
                            RichTextBox1.AppendText("Dynamic DNS: " & IPProp.IsDynamicDnsEnabled.ToString & vbCrLf)
                        End If
    
                        Dim IPv4 As IPv4InterfaceProperties = networkCard.GetIPProperties.GetIPv4Properties
    
                        If Not IPv4 Is Nothing Then
                            RichTextBox1.AppendText("DHCP Enabled: " & IPv4.IsDhcpEnabled.ToString & vbCrLf)
                            RichTextBox1.AppendText("MTU Setting: " & IPv4.Mtu.ToString & vbCrLf)
                            RichTextBox1.AppendText("Uses WINS: " & IPv4.UsesWins.ToString & vbCrLf & vbCrLf & vbCrLf)
                        End If
    
                    End If
                Next
            Next
    
    
        End Sub
    End Class

    Public Class Form1
    
        'Add reference, COM, Network List Manager 1.0 Type Library
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Interval = 10000
            Timer1.Start()
        End Sub
    
        Private Function IsConnectedToInternet() As Boolean
            If System.Environment.OSVersion.Version.Major >= 6 Then
                'Vista and Up
                Dim Networks As New NETWORKLIST.NetworkListManager
                If Networks.IsConnectedToInternet Then
                    Return True
                Else
                    Return False
                End If
            Else
                MessageBox.Show("Your OS is incompatable")
            End If
            Return Nothing
        End Function
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            RichTextBox1.Text = "Is the network connected = " & CStr(IsConnectedToInternet())
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Friday, November 8, 2013 4:52 AM
  • Also that project that enables and disables NICs has to be run with admin privileges as only administrators have high enough rights to enable and disable NICs (at least using WMI apparently). This is what it displays for my laptop when run.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Friday, November 8, 2013 5:03 AM
  • This is a good start. Probably, if the program run as the local system, then it can continue to work even if the user has no admin rights.
    Doing some research, the MSDN mention the usage of Public Shared Event NetworkAvailabilityChanged As NetworkAvailabilityChangedEventHandler. If just need to listen to the physical card or LAN. How could I narrow down to the etherNET ADapter and not the wireless adapter?
    • Edited by PedroRobles Friday, November 8, 2013 11:05 AM
    Friday, November 8, 2013 10:50 AM
  • This is a good start. Probably, if the program run as the local system, then it can continue to work even if the user has no admin rights.
    Doing some research, the MSDN mention the usage of Public Shared Event NetworkAvailabilityChanged As NetworkAvailabilityChangedEventHandler. If just need to listen to the physical card or LAN. How could I narrow down to the etherNET ADapter and not the wireless adapter?

    Is it designed to "narrow down" to 10/100 base t NIC? I doubt it. I would recommend you download the WMI code creator which is free. And then set it to create code in VB.Net. Although it may take you some time to muddle through it to find the correct namespace, class, etc to use for detecting when a NICs interface is no longer has connectivity at the physical layer or whatever. It does create code which can immediately be run by it for testing instead of having to copy the code to visual studio to run. And it also has a tab to do all this for "receive an event" so it will actually create an event handler for the event that you choose you want to be notified of. I am looking at it right now and started up my 10/100 base t connector to see what happens if I turn it on and off. Also I will be connecting my spare WI-FI dongle and establishing a network connection with it then putting foil or a metal pot around it so it loses the WI-FI connection and see what happens.

    WMI code creator is free and it doesn't install anything. When downloaded to the desktop and unzipped you just run it from the unzipped folder. Although I would right click on it and run it with admin privileges (Run as Admin) first and then once you know what you want try again running it as a normal user and see if the code still works. It's actually really simple to use too. I will attempt to get the code to use an event to detect a network down during the next 30 minutes or so and I will display the WMI code creator again, if I succeed, with the code it created and the namespace, class, properties or whatever I picked that it created the code for.

    Anyhow an image of it is below.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Friday, November 8, 2013 11:56 AM
  • Hello,

    I am looking to develop a simple vb.net code that will sense whenever a local area network becomes inactive, and to enable the wireless adapter. Then, it would be nice to have the code verify if all network shares are still available. if not, the code should attempt to initiate reconnecting the users' network shares.

    The goal is allow laptop users to continue having a network connection even if the local area stop working for whatever reason.


    It is always funny that persons ask for difficult problems always simple code. Why do you think that your knowledge is not enough to do difficult code? Then start first with simple problems before taking those things which is often for very well trained programmers difficult to do.

    (It needs a deep knowledge of LAN protocols and more).

     

    Success
    Cor


    Friday, November 8, 2013 12:41 PM
  • Hello,

    I am looking to develop a simple vb.net code that will sense whenever a local area network becomes inactive, and to enable the wireless adapter. Then, it would be nice to have the code verify if all network shares are still available. if not, the code should attempt to initiate reconnecting the users' network shares.

    The goal is allow laptop users to continue having a network connection even if the local area stop working for whatever reason.


    It is always funny that persons ask for difficult problems always simple code. Why do you think that your knowledge is not enough to do difficult code? Then start first with simple problems before taking those things which is often for very well trained programmers difficult to do.

    (It needs a deep knowledge of LAN protocols and more).

     

    Success
    Cor


    How true.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Friday, November 8, 2013 1:05 PM
  • I used this code to get 40 different properties for each Network Adapter and Network Adapter does not mean a NIC necessarily as the list returned contains a ref to a "Microsoft Teredo Tunneling Adapter" (From Yahoo answers - Teredo tunneling is a transition technology that gives full IPv6 connectivity for IPv6-capable hosts which are on the IPv4 Internet but which have no direct native connection to an IPv6 network) which I would believe has nothing to do with hardware as I think that is software that resides somewhere in the IP layers for providing that capability. Although I haven't studied it enough to really know.

    So the code is below, the returned info for my two wireless adapters below that and the WMI code creators showing what I used below that. Couldn't figure out how to establish an event for if a NIC loses network connectivity though. Also put my standby Linksys WI-FI adapter in aluminum foil, then in a can and finally put a metal cooking pot over the can but it didn't lose WI-FI connectivity which I receive from my renter so it's not like I have a local wi-fi router that I can turn off to lose connectivity to see what is returned.

    Also in the info for my wi-fi adapters note that the Atheros returned an entry for being connect to the LAN where the Linksys did not even though the Linsys is connected to the wi-fi network. But the Atheros is being used for my link to the internet, not the Linksys so I suppose that is why that occured. Therefore it's possible that if you can't get an event handler to be used to detect link loss to a LAN by your 10/100 base t NIC you could use a timer to get this info from it and see if the line "NetConnectionID: Local Area Connection"s next line for "NetConnectionStatus: 4" changes when that NIC loses LAN connectivity. Or if that entry no longer gets returned since a LAN is no longer being connected to.

    Of course there's other possible code that WMI can create for other things related to networking which may provide info that what I picked does not. So you may need to look through a lot of information and test code to see what it does. Fortunately there does not appear to be many refs to networks, etc so you shouldn't have to spend maybe more than a couple hours seeing what different code does and testing it relative to the LAN going away and coming back. Also a LAN can be up even though Internet connectivity is gone or the server on the LAN that provides the shares (which I suppose you mean drives that can be shared from the server or something) is down.

    Imports System
    Imports System.Management
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Text = ""
            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NetworkAdapter")
    
                For Each queryObj As ManagementObject In searcher.Get()
    
                    RichTextBox1.AppendText("-----------------------------------" & vbCrLf)
                    RichTextBox1.AppendText("Win32_NetworkAdapter instance" & vbCrLf)
                    RichTextBox1.AppendText("-----------------------------------" & vbCrLf)
                    RichTextBox1.AppendText("NetConnectionID: " & queryObj("NetConnectionID") & vbCrLf)
                    RichTextBox1.AppendText("NetConnectionStatus: " & queryObj("NetConnectionStatus") & vbCrLf)
                    RichTextBox1.AppendText("NetEnabled: " & queryObj("NetEnabled") & vbCrLf)
    
                    If queryObj("NetworkAddresses") Is Nothing Then
                        RichTextBox1.AppendText("NetworkAddresses: " & queryObj("NetworkAddresses") & vbCrLf)
                    Else
                        Dim arrNetworkAddresses As String()
                        arrNetworkAddresses = CType(queryObj("NetworkAddresses"), String())
                        For Each arrValue As String In arrNetworkAddresses
                            RichTextBox1.AppendText("NetworkAddresses: " & arrValue & vbCrLf)
                        Next
                    End If
                    RichTextBox1.AppendText("PermanentAddress: " & queryObj("PermanentAddress") & vbCrLf)
                    RichTextBox1.AppendText("PhysicalAdapter: " & queryObj("PhysicalAdapter") & vbCrLf)
                    RichTextBox1.AppendText("PNPDeviceID: " & queryObj("PNPDeviceID") & vbCrLf)
    
                    If queryObj("PowerManagementCapabilities") Is Nothing Then
                        RichTextBox1.AppendText("PowerManagementCapabilities: " & queryObj("PowerManagementCapabilities") & vbCrLf)
                    Else
                        Dim arrPowerManagementCapabilities As UInt16()
                        arrPowerManagementCapabilities = CType(queryObj("PowerManagementCapabilities"), UShort())
                        For Each arrValue As UInt16 In arrPowerManagementCapabilities
                            RichTextBox1.AppendText("PowerManagementCapabilities: " & arrValue & vbCrLf)
                        Next
                    End If
                    RichTextBox1.AppendText("PowerManagementSupported: " & queryObj("PowerManagementSupported") & vbCrLf)
                    RichTextBox1.AppendText("ProductName: " & queryObj("ProductName") & vbCrLf)
                    RichTextBox1.AppendText("ServiceName: " & queryObj("ServiceName") & vbCrLf)
                    RichTextBox1.AppendText("Speed: " & queryObj("Speed") & vbCrLf)
                    RichTextBox1.AppendText("Status: " & queryObj("Status") & vbCrLf)
                    RichTextBox1.AppendText("StatusInfo: " & queryObj("StatusInfo") & vbCrLf)
                    RichTextBox1.AppendText("SystemCreationClassName: " & queryObj("SystemCreationClassName") & vbCrLf)
                    RichTextBox1.AppendText("SystemName: " & queryObj("SystemName") & vbCrLf)
                    RichTextBox1.AppendText("TimeOfLastReset: " & queryObj("TimeOfLastReset") & vbCrLf)
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub
    
    End Class

    This information returned for my two wireless adapaters

    Atheros adapters info
    
    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: Local Area Connection
    NetConnectionStatus: 4
    NetEnabled: False
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: True
    PNPDeviceID: PCI\VEN_1969&DEV_1062&SUBSYS_04591025&REV_C0\4&3170ED28&0&00E2
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: Atheros AR8132 PCI-E Fast Ethernet Controller (NDIS 6.20)
    ServiceName: L1C
    Speed: 
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420
    
    
    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: Wireless Network Connection
    NetConnectionStatus: 2
    NetEnabled: True
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: True
    PNPDeviceID: PCI\VEN_168C&DEV_002A&SUBSYS_E01F105B&REV_01\4&21D98CC8&0&00E1
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: [CommView] Atheros AR5B93 Wireless Network Adapter
    ServiceName: TS_AR5416
    Speed: 130000000
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420
    
    
    Linsys adapters info
    
    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: Wireless Network Connection 2
    NetConnectionStatus: 2
    NetEnabled: True
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: True
    PNPDeviceID: USB\VID_1737&PID_0078\5&2888D0FF&0&1
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: Linksys RangePlus Wireless USB Network Adapter
    ServiceName: netr28ux
    Speed: 153250000
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420
    


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Friday, November 8, 2013 1:35 PM
  • Well I suppose if I had read this better I would have noticed before my last post that the Teredo thing is in fact not a physical adapter.

    -----------------------------------
    Win32_NetworkAdapter instance
    -----------------------------------
    NetConnectionID: 
    NetConnectionStatus: 
    NetEnabled: 
    NetworkAddresses: 
    PermanentAddress: 
    PhysicalAdapter: False
    PNPDeviceID: ROOT\*TEREDO\0000
    PowerManagementCapabilities: 
    PowerManagementSupported: False
    ProductName: Microsoft Teredo Tunneling Adapter
    ServiceName: tunnel
    Speed: 100000
    Status: 
    StatusInfo: 
    SystemCreationClassName: Win32_ComputerSystem
    SystemName: ACER
    TimeOfLastReset: 20130102051628.375199-420


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    Friday, November 8, 2013 1:41 PM