none
How to find and display MAC Address From .TXT File using VB ?

    Question

  • This file i have have created by using dos cmd i.e. "ipconfig /all >> d:\mac.txt".
    & want to extract MAC Address from this "mac.txt" file by using VB Programing Code. 
    The contents of "mac.txt" file looks like as bellow.... 
    =============================================================
    Windows IP Configuration
       Host Name . . . . . . . . . . . . : Vishu-PC
       Primary Dns Suffix  . . . . . . . : 
       Node Type . . . . . . . . . . . . : Hybrid
       IP Routing Enabled. . . . . . . . : No
       WINS Proxy Enabled. . . . . . . . : No
    Ethernet adapter Tunngle:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : TAP-Win32 Adapter V9 (Tunngle)
       Physical Address. . . . . . . . . : 00-FF-E8-19-92-C0
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
    Ethernet adapter Local Area Connection:
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Realtek PCIe FE Family Controller
       Physical Address. . . . . . . . . : 00-25-22-37-86-87
       DHCP Enabled. . . . . . . . . . . : Yes
       Autoconfiguration Enabled . . . . : Yes
       Link-local IPv6 Address . . . . . : fe80::88ec:415f:4b03:5c03%11(Preferred) 
       IPv4 Address. . . . . . . . . . . : 27.48.141.237(Preferred) 
       Subnet Mask . . . . . . . . . . . : 255.255.252.0
       Lease Obtained. . . . . . . . . . : 28 January 2012 PM 1:49:10
       Lease Expires . . . . . . . . . . : 28 January 2012 PM 4:49:10
       Default Gateway . . . . . . . . . : 27.48.140.1
       DHCP Server . . . . . . . . . . . : 27.48.138.7
       DHCPv6 IAID . . . . . . . . . . . : 234890530
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-16-51-86-4A-00-25-22-37-86-87
       DNS Servers . . . . . . . . . . . : 27.48.138.2
                                           202.62.224.2
                                           202.62.224.5
                                           4.2.2.2
                                           203.98.96.5
                                           202.138.103.100
       NetBIOS over Tcpip. . . . . . . . : Enabled
    Ethernet adapter VMware Network Adapter VMnet1:
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1
       Physical Address. . . . . . . . . : 00-50-56-C0-00-01
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
       Link-local IPv6 Address . . . . . : fe80::2c3f:a342:866c:1324%24(Preferred) 
       IPv4 Address. . . . . . . . . . . : 192.168.142.1(Preferred) 
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 
       DHCPv6 IAID . . . . . . . . . . . : 419450966
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-16-51-86-4A-00-25-22-37-86-87
       DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                           fec0:0:0:ffff::2%1
                                           fec0:0:0:ffff::3%1
       NetBIOS over Tcpip. . . . . . . . : Enabled
    Ethernet adapter VMware Network Adapter VMnet8:
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8
       Physical Address. . . . . . . . . : 00-50-56-C0-00-08
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
       Link-local IPv6 Address . . . . . : fe80::25fe:fa48:ddc9:5157%25(Preferred) 
       IPv4 Address. . . . . . . . . . . : 192.168.27.1(Preferred) 
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 
       DHCPv6 IAID . . . . . . . . . . . : 436228182
       DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-16-51-86-4A-00-25-22-37-86-87
       DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                           fec0:0:0:ffff::2%1
                                           fec0:0:0:ffff::3%1
       NetBIOS over Tcpip. . . . . . . . : Enabled
    Tunnel adapter isatap.{9ADACE2C-4C1B-463C-9D53-2F25FF4C45CF}:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Microsoft ISATAP Adapter
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
    Tunnel adapter 6TO4 Adapter:
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Microsoft 6to4 Adapter
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
       IPv6 Address. . . . . . . . . . . : 2002:1b30:8ded::1b30:8ded(Preferred) 
       Default Gateway . . . . . . . . . : 2002:c058:6301::c058:6301
       DNS Servers . . . . . . . . . . . : 27.48.138.2
                                           202.62.224.2
                                           202.62.224.5
                                           4.2.2.2
                                           203.98.96.5
                                           202.138.103.100
       NetBIOS over Tcpip. . . . . . . . : Disabled
    Tunnel adapter Teredo Tunneling Pseudo-Interface:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
    Tunnel adapter isatap.{F34094C2-0253-4F5D-8C21-BE8A8AA65F1E}:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
    Tunnel adapter isatap.{E81992C0-6ABD-45AB-986C-704D5B3E8494}:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
    Tunnel adapter isatap.{3832DE43-E6C2-4ECB-A5C4-8C31F45E44C9}:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . : 
       Description . . . . . . . . . . . : Microsoft ISATAP Adapter #4
       Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
       DHCP Enabled. . . . . . . . . . . : No
       Autoconfiguration Enabled . . . . : Yes
    ==============================================================
    Please give reply with easy codes because in new with VB and this is my School assignment.....
    Saturday, January 28, 2012 8:56 AM

Answers

  • Hi Bishesh,

    Welcome to the MSDN forums.

    According to your description, it seems that you’ve created a file that contains the information supplied by “ipconfig /all “ command.  If so, you could read the text file and scan all lines in the file like this:

             Imports System.IO

    ….

            Dim macFile() As String = File.ReadAllLines("d:\mac.txt")

            For i = 0 To macFile.Count - 1

                ' The first line that contains "Physical Address" is the MAC address

                If macFile(i).Contains("Physical Address") Then

                    Dim MACAddress() As String = macFile(i).Split(":")

                    Console.WriteLine(MACAddress(1))

                    Exit For

                End If

            Next

     

    Moreover, you could use the getmac command to get the MAC address of local computer. And the  GetPhysicalAddress method could be used to get MAC address without using “ipconfig /all “ command, here is an example:

       Imports System.Net.NetworkInformation

       .

       Dim nic As NetworkInterface

            For Each nic In NetworkInterface.GetAllNetworkInterfaces

                If nic.OperationalStatus = OperationalStatus.Up Then

                    Console.WriteLine(nic.GetPhysicalAddress().ToString)

                    Exit For

                End If

            Next

     

    I hope this will be helpful.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Monday, January 30, 2012 7:43 AM
    Moderator

All replies

  • Is the assignment to get the MAC addresses from the text, or is it to get the MAC addresses?

    If the former, then the first step is to figure out what you can use to identify a line with a MAC address.

    Note that each network interface has its own MAC address, do you want a particular one, and if so, how would you distinguish that from the others?

    --
    Andrew

    Saturday, January 28, 2012 10:48 AM
  • Hi Bishesh,

    Welcome to the MSDN forums.

    According to your description, it seems that you’ve created a file that contains the information supplied by “ipconfig /all “ command.  If so, you could read the text file and scan all lines in the file like this:

             Imports System.IO

    ….

            Dim macFile() As String = File.ReadAllLines("d:\mac.txt")

            For i = 0 To macFile.Count - 1

                ' The first line that contains "Physical Address" is the MAC address

                If macFile(i).Contains("Physical Address") Then

                    Dim MACAddress() As String = macFile(i).Split(":")

                    Console.WriteLine(MACAddress(1))

                    Exit For

                End If

            Next

     

    Moreover, you could use the getmac command to get the MAC address of local computer. And the  GetPhysicalAddress method could be used to get MAC address without using “ipconfig /all “ command, here is an example:

       Imports System.Net.NetworkInformation

       .

       Dim nic As NetworkInterface

            For Each nic In NetworkInterface.GetAllNetworkInterfaces

                If nic.OperationalStatus = OperationalStatus.Up Then

                    Console.WriteLine(nic.GetPhysicalAddress().ToString)

                    Exit For

                End If

            Next

     

    I hope this will be helpful.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Monday, January 30, 2012 7:43 AM
    Moderator