Microsoft > Forums Home > .NET Framework Networking and Communication > C# - How to detect all IP addresses from a LAN?

Answered C# - How to detect all IP addresses from a LAN?

  • Thursday, March 13, 2008 6:55 AM
     
     
    Hello,

    I am trying to write a C# program to detect all the IP addresses from a LAN. So far, I can only detect my local PC IP using the codes below. Can someone help provide a sample code on how to detect all the other IP addresses connected to the LAN?

    CODE:

    string strHostName = string.Empty;

    cmbIPAddress.Items.Clear();
                   
    // Getting Ip address of local machine...
    // First get the host name of local machine.

    strHostName = Dns.GetHostName();

     // Then using host name, get the IP address list..

    IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
    IPAddress[] iparrAddr = ipEntry.AddressList;

    if (iparrAddr.Length > 0)
    {
       for (int intLoop = 0; intLoop < iparrAddr.Length; intLoop++)
       cmbIPAddress.Items.Add(iparrAddr[intLoop].ToString());
    }

    Thanks!

Answers

  • Sunday, March 16, 2008 1:55 AM
    Moderator
     
     Answered

    As you can imagine there are no such API's for direct consumption, as this would be a security leak.

     

    One thing you may consider doing is to ICMP ping within your local subnet in an incremental way, and collect the list of replies.

     

    Though most up to date machines with their firewall turned on, will not answer ICMP ping's.

     

All Replies

  • Sunday, March 16, 2008 1:55 AM
    Moderator
     
     Answered

    As you can imagine there are no such API's for direct consumption, as this would be a security leak.

     

    One thing you may consider doing is to ICMP ping within your local subnet in an incremental way, and collect the list of replies.

     

    Though most up to date machines with their firewall turned on, will not answer ICMP ping's.

     

  • Wednesday, March 19, 2008 1:27 AM
     
     
    Hello,

    Thanks for the feedback. I will try the ICMP ping method then.

    BR//
  • Thursday, October 16, 2008 7:36 AM
     
     Proposed Answer Has Code

    Hi,

     

    You could use a utility provided by Windows and get the list of IP addresses on LAN. (Note: This utility is available on Windows, for other OS you might like to use some other analogous utility)

    Code:

    using System.Diagnostics; 
     
    using System.IO; 
     
      
     
      
     
    //Gets the machine names that are connected on LAN 
     
    Process netUtility = new Process(); 
     
    netUtility.StartInfo.FileName = "net.exe"
     
    netUtility.StartInfo.CreateNoWindow = true
     
    netUtility.StartInfo.Arguments = "view"
     
    netUtility.StartInfo.RedirectStandardOutput = true
     
    netUtility.StartInfo.UseShellExecute = false
     
    netUtility.StartInfo.RedirectStandardError = true
     
    netUtility.Start(); 
     
      
     
    StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream, netUtility.StandardOutput.CurrentEncoding); 
     
      
     
    string line = ""
     
    while ((line = streamReader.ReadLine()) != null) 
     
     
          if (line.StartsWith("\\")) 
     
          { 
     
               listBox1.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper()); 
     
          } 
     
     
    streamReader.Close(); 
    netUtility.WaitForExit(1000); 
     

     

    Hope this works.


    Cheers!!!

    Rajshree

    (rajshreedugar@hotmail.com)

    • Proposed As Answer by Rajshree Dugar Thursday, October 16, 2008 7:36 AM
    •  
  • Thursday, October 16, 2008 2:01 PM
     
     Proposed Answer
    if this is your local LAN then use the method proposed by Savas, if you are going off net be careful.  some of us consider that hacking.
    • Proposed As Answer by san2011 Friday, October 07, 2011 7:41 AM
    •  
  • Saturday, March 27, 2010 4:10 PM
     
     Proposed Answer

    thank u so much for ur help

    now iam try to connect lan machine sql databse it doesnt connect.

    if u know please replay me

    • Proposed As Answer by shiva_n Saturday, March 27, 2010 4:10 PM
    •