tags:

views:

475

answers:

2

how to get host name of all LAN systems using c#.net

through "net view" command am getting some systems host names, not able to access all the systems even though they are in the LAN. why so?

+1  A: 

Try this: (For no Active directory)

public static int GetAllIPAndHostNames()
        {
            string strHostName;

            // Getting Ip address of local machine...
            // First get the host name of local machine.
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine's Host Name: " + strHostName);

            IPHostEntry remoteIP;

            //using host name, get the IP address list..
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            int i = 0;
            while (i < addr.Length)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
                //HostNames
                remoteIP = Dns.GetHostEntry((addr[i]));
                Console.WriteLine("HostName {0}: {1} ", i, remoteIP.HostName);
                i++;
            }
            return 0;
        }
Aseem Gautam
Just a small suggestion, would it be adviseable to use a for loop instead of a while loop? As in for(int i = 0; i < addr.Count; i++).
Jamie Keeling
A: 

But, still I'm getting my local machine's hostname & it's IP. How would I get all the active IPs in my LAN? I've also tried with 'ManagementObjectSearcher', but the result is same. So, please help me asap as I'm in desperate position for sometime...

Samiul