Results 1 to 12 of 12

Thread: Gathering Local PC information

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Gathering Local PC information

    Im trying to gather the following information using VB.NET and .NET 2

    -IP Address - I havent seen any usefull resources about the .NET framework with IP Address collecting

    -MAC Address

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Gathering Local PC information

    You can definitely use WMI to get the MAC addresses of any network adapters in your machine:
    VB Code:
    1. Dim networkAdapters As New Management.ManagementClass("Win32_NetworkAdapter")
    2.  
    3.         For Each networkAdapter As Management.ManagementObject In networkAdapters.GetInstances()
    4.             MessageBox.Show(String.Format("Name: {1}{0}Type: {2}{0}MAC Address: {3}", _
    5.                                           Environment.NewLine, _
    6.                                           networkAdapter("Name"), _
    7.                                           networkAdapter("AdapterType"), _
    8.                                           networkAdapter("MACAddress")))
    9.         Next networkAdapter
    Note that this requires a reference to System.Management. The IP address I don't know as I've never had the pleasure, but the Win32_NetworkAdapterConfiguration class has a property named IPAddress that is a string array.

    Edit:
    Search for "Win32_NetworkAdapter" on MSDN for more information on that class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Gathering Local PC information

    VB Code:
    1. ' *****VB2005*****
    2. Option Strict On
    3.  
    4. Public Class Form1
    5.  
    6.     ' add reference to system.management
    7.     Private listview1 As New ListView
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         Me.Controls.Add(listview1)
    11.         GetNetworkInfo()
    12.     End Sub
    13.  
    14.     Private Sub GetNetworkInfo()
    15.  
    16.         ' storing Network adapters in this:
    17.         Dim adapters As New ArrayList
    18.  
    19.         Dim pDataCol As System.Management.PropertyDataCollection
    20.         Dim pData As System.Management.PropertyData
    21.         ' Get info from the WIN32_NetworkAdapterConfiguration Class
    22.         Dim networkAdapterConfigurationClass As New System.Management.ManagementClass("Win32_NetworkAdapterConfiguration")
    23.         Dim configurationInstances As Management.ManagementObjectCollection = networkAdapterConfigurationClass.GetInstances
    24.         Dim configurationInstance As Management.ManagementObject
    25.  
    26.         ' Loop through each instance (in this case each adapter)
    27.         For Each configurationInstance In configurationInstances
    28.             pDataCol = configurationInstance.Properties
    29.             pData = pDataCol.Item("IPAddress")
    30.             ' skip anything without an IP:
    31.             If pData.Value Is Nothing Then Continue For
    32.  
    33.             ' we are storing the information in a structure called NetworkAdapter defined later...
    34.             Dim anotherAdapter As New NetworkAdapter
    35.  
    36.             ' There could be more than 1 IP, so the this is an array:
    37.             anotherAdapter.IPAddresses = DirectCast(pData.Value, String())
    38.  
    39.             ' Get the remaining info for this adapter
    40.             ' To get the name we need to use another management class, it also has
    41.             ' the index property of the adapters, so we can find this adapter from its index
    42.             pData = pDataCol.Item("Index")
    43.             If pData.Value IsNot Nothing Then
    44.                 ' store the index:
    45.                 anotherAdapter.Index = pData.Value.ToString
    46.                 ' we have a function to get the name:
    47.                 anotherAdapter.Name = GetName(Convert.ToUInt32(pData.Value))
    48.             End If
    49.             ' finally MACAddress:
    50.             pData = pDataCol.Item("MACAddress")
    51.             If pData.Value IsNot Nothing Then
    52.                 anotherAdapter.MACAddress = pData.Value.ToString
    53.             End If
    54.             ' store the NetworkAdapter in the arraylist
    55.             adapters.Add(anotherAdapter)
    56.         Next
    57.  
    58.         If adapters.Count = 0 Then Exit Sub ' no network adapters!
    59.  
    60.         FillListView(adapters)
    61.  
    62.     End Sub
    63.  
    64.     Private Function GetName(ByVal index As UInteger) As String
    65.         ' Friendly name is in a different management class
    66.         ' We'll match adapters with the index
    67.         Dim adaptername As String = ""
    68.         Dim pDataCol As System.Management.PropertyDataCollection
    69.         Dim pData As System.Management.PropertyData
    70.         ' same as other class really...
    71.         Dim networkAdapterClass As New System.Management.ManagementClass("Win32_NetworkAdapter")
    72.         Dim networkAdapterInstances As Management.ManagementObjectCollection = networkAdapterClass.GetInstances
    73.         Dim networkAdapterInstance As Management.ManagementObject
    74.         For Each networkAdapterInstance In networkAdapterInstances
    75.             pDataCol = networkAdapterInstance.Properties
    76.             pData = pDataCol.Item("Index")
    77.  
    78.             If index = Convert.ToUInt32(pData.Value) Then
    79.                 ' indexes match so this is the same adapter, so get its name
    80.                 pData = pDataCol.Item("Name")
    81.                 If pData.Value IsNot Nothing Then
    82.                     adaptername = pData.Value.ToString
    83.                     Exit For
    84.                 End If
    85.             End If
    86.         Next
    87.  
    88.         Return adaptername
    89.  
    90.     End Function
    91.  
    92.     Private Structure NetworkAdapter
    93.         ' store info for each adapter:
    94.         Public Index As String
    95.         Public MACAddress As String
    96.         Public IPAddresses() As String
    97.         Public Name As String
    98.     End Structure
    99.  
    100.     Private Sub FillListView(ByVal adapters As ArrayList)
    101.  
    102.         ' display results:
    103.         Me.listview1.Dock = DockStyle.Fill
    104.         Me.listview1.View = View.Details
    105.         Me.listview1.Columns.Add("Name", -1, HorizontalAlignment.Left)
    106.         Me.listview1.Columns.Add("IPAddress", -1, HorizontalAlignment.Left)
    107.         Me.listview1.Columns.Add("MACAddress", -1, HorizontalAlignment.Left)
    108.  
    109.         For i As Integer = 0 To adapters.Count - 1
    110.             Dim adapter As NetworkAdapter = DirectCast(adapters(i), NetworkAdapter)
    111.             Dim lvi As New ListViewItem(adapter.Name)
    112.             Dim s As String = ""
    113.             Dim count As Integer = 0
    114.             Do
    115.                 s &= adapter.IPAddresses(count)
    116.                 count += 1
    117.             Loop Until count = adapter.IPAddresses.Length
    118.  
    119.             lvi.SubItems.Add(s)
    120.             lvi.SubItems.Add(adapter.MACAddress)
    121.             Me.listview1.Items.Add(lvi)
    122.         Next
    123.  
    124.         For i As Integer = 0 To 2
    125.             Me.listview1.Columns(i).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
    126.         Next
    127.  
    128.     End Sub
    129.  
    130. End Class

    You said .net 2, so I've used vb05 stuff. It's easy enough to translate back to vb03 if thats what you have
    Last edited by jo0ls; Nov 17th, 2005 at 05:06 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Gathering Local PC information

    Quote Originally Posted by jo0ls
    [Highlight=VB]
    ' *****VB2005*****


    You said .net 2, so I've used vb05 stuff. It's easy enough to translate back to vb03 if thats what you have

    .NET 2 is part of VS2005

  5. #5
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Gathering Local PC information

    Yes, I was trying to be clear about what it would compile on, and failed miserably...
    It works in vb express 2005, so it will work in any visual basic 2005. It will not work with older versions of the framework as I used IsNot, listview.column.autoresize and a UInteger. So to convert it to work on earlier versions is trivial.
    .Net 2 isn't really part as VS2005, you can download just the framework sdk and write programs in notepad if you really want.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Gathering Local PC information

    I wish MSFT would make it simple like: my.computer.network.localip and my.computer.network.mac

    but MSFT would never do that :P

  7. #7
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Gathering Local PC information

    Here's another way to get just the IP address I used once before..
    VB Code:
    1. Dim PCName as String = Dns.GetHoseName.ToString
    2.         Dim myIPHost As System.Net.IPHostEntry = Dns.Resolve(PCName)
    3.         Dim myIP As System.Net.IPAddress = myIPHost.AddressList(0)
    4.         MsgBox(myIP.ToString)

    It'll also tell you the PC name.. His way is more thorough though.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Gathering Local PC information

    Quote Originally Posted by tylerm
    I wish MSFT would make it simple like: my.computer.network.localip and my.computer.network.mac

    but MSFT would never do that :P
    When I use the method I suggested I get about 6 or 7 network adapters with MAC addresses.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Gathering Local PC information

    Code:
            Dim networkAdapters As New Management.ManagementClass("Win32_NetworkAdapter")
            For Each networkAdapter As Management.ManagementObject In networkAdapters.GetInstances()
                net_mac.Text = String.Format("Name: {1}{0}Type: {2}{0}MAC Address: {3}", _
                                              Environment.NewLine, _
                                              networkAdapter("Name"), _
                                              networkAdapter("AdapterType"), _
                                              networkAdapter("MACAddress"))
            Next networkAdapter
    It displays the Name but the Type and Address do not appear.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Gathering Local PC information

    That's why I coded it the way I did: to show that there are a number of network adapters of different types in most machines. You need to use the properties of each to determine which one(s) you are interested in. I changed my code to append each output to a TextBox and here's what I got:
    Name: 1394 Net Adapter
    Type: Ethernet 802.3
    MAC Address: 82:F3:C8:A6:41:BD

    Name: RAS Async Adapter
    Type:
    MAC Address:

    Name: WAN Miniport (L2TP)
    Type:
    MAC Address:

    Name: WAN Miniport (PPTP)
    Type: Wide Area Network (WAN)
    MAC Address: 50:50:54:50:30:30

    Name: WAN Miniport (PPPOE)
    Type: Wide Area Network (WAN)
    MAC Address: 33:50:6F:45:30:30

    Name: Direct Parallel
    Type:
    MAC Address:

    Name: WAN Miniport (IP)
    Type:
    MAC Address:

    Name: Packet Scheduler Miniport
    Type: Ethernet 802.3
    MAC Address: EC:2D:20:52:41:53

    Name: Microsoft Tun Miniport Adapter
    Type: Ethernet 802.3
    MAC Address: 02:00:54:55:4E:01

    Name: NVIDIA nForce Networking Controller
    Type: Ethernet 802.3
    MAC Address: 00:50:8D:D7:89:6D

    Name: Packet Scheduler Miniport
    Type: Ethernet 802.3
    MAC Address: 00:50:8D:D7:89:6D
    It would only be those with a type of "Ethernet 802.3" that you would be interested in, and I'm guessing that all the "Miniport" ones are software devices rather than hardware. Obviously the 1394 adapter is a FireWire connection, which leaves the second last item as the primary network adapter. This logic is not definitive, so you may need to do some more research to make sure you are using the best criteria to identify the primary network adapter, but I wanted to point out that it is not just a straightforward "GET MAC ADDRESS" kind of thing. There may also be other properties of the Win32_NetworkAdapter class, or indeed other classes, like Win32_NetworkAdapterConfiguration, that will help you to identify just the one you're looking for. I don't know if all the 802.3 adapters would have an IP address assigned, but you could use jo0ls code to work that out, maybe with slight modification to exclude all items with a type other than "Ethernet 802.3".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Gathering Local PC information

    I just excluded everything without an IP. Attached is a version that gets a few more fields, and which doesn't filter. Another thing to look for to find the primary adapter is the DefaultIPGateway value.
    Attached Files Attached Files

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Gathering Local PC information

    I didn't look too closely at your code jo0ls. Another thing to note is that you can actually generate .NET classes that correspond to WMI classes automatically using the mgmtclassgen.exe utility. That way you can use strongly-typed objects instead of indexing properties of ManagementObject instances.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width