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

, 6 May 2004
Rate this:
Please Sign up or sign in to vote.
This will enumerate all of the IP Addresses on the network without using ActiveDirectory.

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

Share

About the Author

MichaelCoder

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMember 654289910-Sep-09 15:21 
GeneralMy vote of 1 Pinmemberlegion_9-Jul-09 11:38 
GeneralSuggestion PinmemberJames Thresher9-May-04 20:16 
GeneralRe: Suggestion PinmemberMichaelCoder11-May-04 12:55 
GeneralRe: Suggestion PinmemberJames Thresher11-May-04 19:35 
GeneralRe: Suggestion PinmemberMichaelCoder12-May-04 9:43 
GeneralRe: Suggestion PinmemberJames Thresher12-May-04 21:07 
GeneralRe: Suggestion Pinmembermdsicignano12-Sep-07 5:00 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 7 May 2004
Article Copyright 2004 by MichaelCoder
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid