Share via


How to get machine name and IP address within our LAN? (C# Win application)

Question

Thursday, September 2, 2010 11:45 AM

Hi all,

   In one simple LAN environment (with or without DNS server), how to get the system/machine name and IP address of all the systems in the network for C# windows application? If we use DNS class from system name space, it gives me the Internet service provider's IP address for me! But I want to list my colleague's machine's ID address. Please help me.

RVandakar

All replies (3)

Thursday, September 2, 2010 3:26 PM ✅Answered

You can try something like this, although I'm sure there are better ways to do it, it's also possible to write a small ping class to make this process more flexible, and efficient.

 

namespace ConsoleApplication2
{
    using System.Net.NetworkInformation;
    using System;
    using System.Net;

    class Program
    {
        static void Main(string[] args)
        {
            if(NetworkInterface.GetIsNetworkAvailable())
            {
                IPInterfaceProperties ipProps;
                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (((nic.OperationalStatus == OperationalStatus.Up) && (nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel)) && (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback))
                    {
                        ipProps = nic.GetIPProperties();

                        byte[] addr;
                        string ip;

                        foreach (GatewayIPAddressInformation gip in ipProps.GatewayAddresses)
                        {
                            addr = gip.Address.GetAddressBytes();

                            for (int i = byte.MinValue; i <= byte.MaxValue; i++)
                            {
                                addr.SetValue((byte)i, 3);
                                ip = string.Format("{0}.{1}.{2}.{3}", addr[0], addr[1], addr[2], addr[3]);

                                Dns.BeginGetHostAddresses(ip, new AsyncCallback(RequestHostAddress), new IPAddress(addr));
                            }
                        }
                    }                   
                }
            }
            
            Console.ReadKey();
        }

        private static void RequestHostAddress(IAsyncResult r)
        {
            IPAddress ipAddr = (IPAddress)r.AsyncState;

            try
            {
                Console.WriteLine(Dns.GetHostEntry(ipAddr).HostName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine(ipAddr.ToString());
            }
            finally
            {
                Dns.EndGetHostAddresses(r);
            }
        }
    }
}

 

Eyal, Regards.

commandbrowser.eyal7.net blog.eyal7.net


Monday, September 6, 2010 12:35 PM ✅Answered

Hi Rajeev Vandakar,

 

Welcome to MSDN Forum!

 

The demo following will help you to collect all computer information in local.

 

If there’s an expection threw by Dns.GetHostByAddress(myScanIP), it is telling us that this ip is not alive now, so the application will continue to scan the next ip.

 

    public class AllPc
    {
        private int startIP = 0;
        private int endIP = 0;
        private string ipPrefix = "";
        private ArrayList computerList = null;
 
        public AllPc(string ipPrefix, int startIP, int endIP)
        {
            this.startIP = startIP;
            this.endIP = endIP;
            this.ipPrefix = ipPrefix;
            computerList = new ArrayList();
        }
        public void ScanComputers()
        {
            for (int i = startIP; i <= endIP; i++)
            {
                string scanIP = ipPrefix + "." + i.ToString();
               IPAddress myScanIP = IPAddress.Parse(scanIP);
                IPHostEntry myScanHost = null;
                string[] arr = new string[2];
                try
                {
                    myScanHost = Dns.GetHostByAddress(myScanIP);
                }
                catch
                {
                    continue;
                }
                if (myScanHost != null)
                {
                    arr[0] = myScanHost.HostName;
                    arr[1] = scanIP;
                    computerList.Add(arr);
                    Console.Write(myScanHost.HostName.ToString()+"\t");
                    Console.WriteLine(scanIP.ToString());
                }
            }
        }
        public static void Main()
        {
            AllPc all = new AllPc("192.168.1", 0, 255);
            all.ScanComputers();
            Console.ReadLine();
        }
    }

 

If there’s anything unclear or I have misunderstood your meaning, please feel free to let me know. And I’m glad to help you.

 

 

Have a nice day!

Mike

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.


Thursday, September 2, 2010 3:00 PM

See http://msdn.microsoft.com/en-us/library/s128tyf6(v=VS.100).aspxMy next phone is Windows Phone 7