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();
string MyAdd =
Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo();
psi.FileName = @"C:\WINDOWS\system32\cmd.exe";
psi.Arguments = "/c net view > boo.txt";
psi.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
Application.DoEvents();
System.Diagnostics.Process.Start(psi);
System.IO.StreamReader sr = null;
bool run = false;
while(run == false)
{
Application.DoEvents();
try
{
System.Threading.Thread.Sleep(1000);
sr = new System.IO.StreamReader(Application.StartupPath
+ "\\boo.txt");
run = true;
}
catch
{
run = false;
}
}
while(sr.ReadLine().StartsWith("--") != true)
{
Application.DoEvents();
}
string str = "";
string[] comp = new string[32];
int i = 0;
while(str.StartsWith("The") != true)
{
Application.DoEvents();
str = sr.ReadLine();
comp[i] = str.Split(char.Parse(" "))[0];
comp[i] = comp[i].Substring(2, comp[i].Length - 2);
if(comp[i] == "e")
{
Application.DoEvents();
comp[i] = null;
}
i++;
}
sr.Close();
sr = null;
foreach(string s in comp)
{
if(s != null)
{
if(s.ToUpper() != Dns.GetHostName().ToUpper())
{
Addresses.Add(Dns.GetHostByName(s).AddressList[0].ToString());
}
}
}
return Addresses;
}
All you have to do is call this function. E.g.:
ArrayList Adds = new ArrayList();
Adds = GetIPAddresses();