Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
1.36/5 (19 votes)
6 May 2004 62.2K   26   8
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

C#
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.:

C#
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
W4Rl0CK4710-Sep-09 15:21
W4Rl0CK4710-Sep-09 15:21 
f
GeneralMy vote of 1 Pin
legion_9-Jul-09 11:38
legion_9-Jul-09 11:38 
GeneralSuggestion Pin
James Thresher9-May-04 20:16
James Thresher9-May-04 20:16 
GeneralRe: Suggestion Pin
MichaelCoder11-May-04 12:55
MichaelCoder11-May-04 12:55 
GeneralRe: Suggestion Pin
James Thresher11-May-04 19:35
James Thresher11-May-04 19:35 
GeneralRe: Suggestion Pin
MichaelCoder12-May-04 9:43
MichaelCoder12-May-04 9:43 
GeneralRe: Suggestion Pin
James Thresher12-May-04 21:07
James Thresher12-May-04 21:07 
GeneralRe: Suggestion Pin
mdsicignano12-Sep-07 5:00
mdsicignano12-Sep-07 5:00 

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

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