Getting IP Address
Getting the Ip address of the local machine
using system.net;
string strHostName = string.Empty;
cmbIPAddress.Items.Clear();//combo box to hold the ip address
strHostName = Dns.GetHostName();//get name of the local machine
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());
}
Getting the ip addresses of machines connected through LAN
First Get the names of the machines
using System.Net;
using System.Diagnostics;
using System.IO;
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("\\"))
{
cmbnetworkcomputers.Items.Add(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper());//cmbnetworkcomputers is a combobox
}
}
streamReader.Close();
netUtility.WaitForExit(1000);
Now to get the ip addrresses,use these names
for (int i = 0; i < cmbnetworkcomputers.Items.Count; i++)
{
string st;
st = cmbnetworkcomputers.Items[i].ToString();
IPHostEntry ipEntry = Dns.GetHostByName(st);
IPAddress[] iparrAddr = ipEntry.AddressList;
if (iparrAddr.Length > 0)
{
for (int intLoop = 0; intLoop < iparrAddr.Length; intLoop++)
cmbIPAddress.Items.Add(iparrAddr[intLoop].ToString());//combobox to hold Ip Address
}
}
No related posts.








Leave your response!