Click here to Skip to main content
Click here to Skip to main content

Enumerate All of the IP Addresses on the Network W/O ActiveDirectory

By , 6 May 2004
 

Introduction

This code gets all of the IP addresses on the network. The best part is, it doesn't use ActiveDirectory. It uses the default DOS commands for networking, specifically "net view". Also, it uses the System.Net.Dns class to resolve the IPs.

Code

private ArrayList GetIPAddresses()
{
 ArrayList Addresses = new ArrayList(); // Temp array to fill
 //Get my address
 string MyAdd = 
  Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
 // Used to start the command
 System.Diagnostics.ProcessStartInfo psi = 
       new System.Diagnostics.ProcessStartInfo();
 //prompt
 psi.FileName = @"C:\WINDOWS\system32\cmd.exe"; // Path for the cmd prompt
 psi.Arguments = "/c net view > boo.txt"; // Arguments for the command prompt
 // "/c" tells it to run the following command which is "net view > boo.txt"
 // "net view" lists all the computers on the network
 // " > boo.txt" tells dos to put the results in a file called boo.txt
 psi.WindowStyle = 
  System.Diagnostics.ProcessWindowStyle.Hidden; // Hide the window
 Application.DoEvents();
 // So the computer doesn't bog down
 System.Diagnostics.Process.Start(psi); // Run
 System.IO.StreamReader sr = null; // used for boo.txt
 bool run = false; // used to tell if dos is done yet
 while(run == false) // while dos is still running
 {
  Application.DoEvents(); // let windows do it's stuff
  try
  {
   System.Threading.Thread.Sleep(1000); // wait a second
   sr = new System.IO.StreamReader(Application.StartupPath 
                   + "\\boo.txt"); // Try to open the file
   // If dos is not done yet, this will throw an error
   run = true; // If I get here, dos is done and we can use the file
  }
  catch
  {
   run = false; // Dos is not done yet
  }
 }
 // Cycle through the file until the info starts
 while(sr.ReadLine().StartsWith("--") != true) 
 {
   Application.DoEvents();
 }
 string str = ""; // filled with each line of the file
 string[] comp = new string[32]; // temp array of computer names
 int i = 0; // indexer for comp[]
 // cycle until we reach, "The command completed successfully."
 while(str.StartsWith("The") != true) 
 {
   Application.DoEvents();
   str = sr.ReadLine(); // Read the next line
  // Split the line after the first space
   comp[i] = str.Split(char.Parse(" "))[0];
   // Normally, in the file it lists it like this
   // \\MyComputer                 My Computer's Description

  // Take off the slashes, "\\MyComputer" to "MyComputer"
   comp[i] = comp[i].Substring(2, comp[i].Length - 2);
   if(comp[i] == "e") 
  // For some reason, I was getting an element
  // in this array that has the value of "e", so I took 
  // it out! lol
   {
     Application.DoEvents();
     comp[i] = null;
   }
   i++; // move to the next place in comp[]
 }
 sr.Close(); // close the file handle
 sr = null; // free memory
 foreach(string s in comp) // for every computer that we have...
 {
  if(s != null) // if there really is a computer...
  {
   if(s.ToUpper() != Dns.GetHostName().ToUpper()) 
  // if the computer is not me...
   {
    Addresses.Add(Dns.GetHostByName(s).AddressList[0].ToString()); 
  // ... add the address to the array
   }
  }
 }
 return Addresses; // return the array
}

All you have to do is call this function. E.g.:

ArrayList Adds = new ArrayList();
Adds = GetIPAddresses();

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MichaelCoder
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberMember 654289910 Sep '09 - 15:21 
GeneralMy vote of 1memberlegion_9 Jul '09 - 11:38 
GeneralSuggestionmemberJames Thresher9 May '04 - 20:16 
GeneralRe: SuggestionmemberMichaelCoder11 May '04 - 12:55 
GeneralRe: SuggestionmemberJames Thresher11 May '04 - 19:35 
GeneralRe: SuggestionmemberMichaelCoder12 May '04 - 9:43 
GeneralRe: SuggestionmemberJames Thresher12 May '04 - 21:07 
GeneralRe: Suggestionmembermdsicignano12 Sep '07 - 5:00 
Agree that this is a "better way" than shelling out to Net View, but the problem that I see even with NetServerEnum is that it isn't 100% reliable for returning a list of IP addresses. We wrote something using NetServerEnum and when we turn off File and Print sharing, the machines disappaer. They are no longer seen by the network browser service. TCP/IP is still up and running. The machine is still pingable... But NetServerEnum no longer returns it.
 
The NetServerEnum method relies on one network protocol (Microsoft Network) to come up with a list of machines available on another network (TCP/IP).
 
I would like a TCP/IP only solution.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 7 May 2004
Article Copyright 2004 by MichaelCoder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid