Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone!

I'm Haseeb,

I want to display the IPAddress of all the PC's in a listbox that are connected via LAN.

I have a piece of code but it couldn't display the IPAddress of PC's connected to LAN in my computer lab

C#
hostname = Dns.GetHostName();
            IPHostEntry IPentry = Dns.GetHostEntry(hostname);
            IPAddress[] addr = IPentry.AddressList;
            string addres = addr[0].ToString();
            foreach (IPAddress myip in IPentry.AddressList)
            {
                listBox1.Items.Add(myip.ToString());
            }
            listBox1.Items.Remove(addres);


Please help me in getting the IPAddresses
I'll be highly thankful to you.
Posted
Updated 29-Jul-10 22:05pm
v2
Comments
Dalek Dave 30-Jul-10 4:05am    
Minor Edit for Spelling and Readability

I remeber using windows API method , getting host names which you can resolve to IP addresses. but that will only work for computers in domain I think.
here it is
C#
[DllImport("Netapi32.dll")]
private static extern int NetServerEnum(
    IntPtr     servername,
    uint       level,
    out IntPtr bufptr,
    int        prefmaxlen,
    out int    entriesread,
    out int    totalentries,
    uint       servertype,
    IntPtr     domain,
    IntPtr     resume_handle);


and this code using it


C#
public static string[] EnumComputers()
        {
            IntPtr pInfo;

            int entriesRead = 0;
            int totalEntries = 0;

            int result = NetServerEnum(
                IntPtr.Zero,
                LEVEL_SERVER_INFO_100,
                out pInfo,
                MAX_PREFERRED_LENGTH,
                out entriesRead,
                out totalEntries,
                SV_TYPE_WORKSTATION | SV_TYPE_SERVER,
                IntPtr.Zero,
                IntPtr.Zero);

            if (result != 0)
            {
                throw new ApplicationException("NetApi Error = " + String.Format("0x{0,0:X}", result));
            }

            string[] computers = new string[entriesRead];

            IntPtr pos = pInfo;

            for (int ii = 0; ii < entriesRead; ii++)
            {
                SERVER_INFO_100 info = (SERVER_INFO_100)Marshal.PtrToStructure(pos, typeof(SERVER_INFO_100));

                computers[ii] = info.sv100_name;

                pos = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(SERVER_INFO_100)));
            }

            NetApiBufferFree(pInfo);

            return computers;
        }
 
Share this answer
 
v2
Comments
haseebsvirgo 30-Jul-10 4:22am    
no
haseebsvirgo 31-Jul-10 5:31am    
any easy solution except this .....
it will be highly appriciated ....
Try look at IPGlobalProperties class and GetActiveTcpConnections

IPGlobalProperties

This gives you a overview of all active Tcp connection to your own computer.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900