Get IPAddresses on a local Network using `Directory Entry`

1.7k views Asked by At

I have to develop an desktop app which shall allow me file sharing on a local network.For this, I am able to get a listview of devices hostname but when it comes to know their IP Addresses and MAC I went to see System.netand several other MSDN forums (where the help was available for metro apps).

How can i get the IP addresses & MAC Address of all devices on my local network?

I am using DirectoryEntry to get User Names and get displayed in listview.

   lstLocal.Items.Clear();
        lstLocal.View = View.Details;
        lstLocal.FullRowSelect = true;

        DirectoryEntry root = new DirectoryEntry("WinNT:");

        foreach (DirectoryEntry computers in root.Children)
        {
            foreach (DirectoryEntry computer in computers.Children)
            {
                if (computer.Name != "Schema")
                {
                    ListViewItem item = new ListViewItem(computer.Name);
                   // item.SubItems.Add(computer.Name);

                    //MessageBox.Show(computer.Name);
                    lstLocal.Items.Add(item);

                }

            }
        }

UPDATE: I used

                     var hostname = Dns.GetHostName();
                    var ipadd = Dns.GetHostAddresses(hostname);

but the address is in IPV6 returned.I need it in IPV4.

4

There are 4 answers

2
Mitz On

There are inbuilt classes to find the IP/Mac addresses. Please check this link.

Snippet taken from above:

System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
                foreach(IPAddress TempA in TempAd)
                {
                    Ipaddr[1] = TempA.ToString();

                    byte[] ab = new byte[6];
                    int len = ab.Length;

                    // This Function Used to Get The Physical Address
                    int r = SendARP( (int) TempA.Address, 0, ab, ref len );
                    string mac = BitConverter.ToString( ab, 0, 6 );

                    Ipaddr[2] = mac;
                }           
1
Heinzi On

If you already have the host names, you are almost done.

You can

0
Saket Kumar On

You can try this:

For IP Address:

var hostname = Dns.GetHostName();
var ipadd = Dns.GetHostAddresses(hostname); //array will contain ipv4 and ipv6

For MAC Address:

    public string GetMACAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
            if (sMacAddress == String.Empty)// only return MAC Address from first card  
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                sMacAddress = adapter.GetPhysicalAddress().ToString();
            }
        } return sMacAddress;
    }

Reference for MAC Address:

MAC Address

0
Mohd Iliyas On

Using WMI mac address can be detected "machine" is your machine name. Add a reference to System.Management in your project

ManagementScope scope = new ManagementScope();
var options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.Default;
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;

scope = new ManagementScope(@"\\" + machine + "\\root\\CIMV2", options);
scope.Connect();
SelectQuery query = new SelectQuery("Select * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher objMOS = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objMOC = objMOS.Get();
string macAddress = String.Empty;
foreach (ManagementObject objMO in objMOC) {
    object tempMacAddrObj = objMO["MacAddress"];

    if (tempMacAddrObj == null) {
        continue;
    }
    if (macAddress == String.Empty) {
        macAddress = tempMacAddrObj.ToString();
    }
    objMO.Dispose();
}

return macAddress;