参考代码
获得局域网的主机名
你必须先得到局域网内所有的IP地址(或IP地址范围),然后根据IP地址得到主机名。
例如:
string ipMask = "***.***.***.";
for (int i = 128; i<= 255; i++)
{
string ipAddress = ipMask + i.ToString() ;
IPHostEntry iphost = new IPHostEntry();
try
{
iphost = Dns.Resolve(ipAddress); IPAddress[] ipp1 = iphost.AddressList;
foreach(IPAddress ip in ipp1)
{
listBox1.Items.Add("主机名称:"+iphost.HostName );
listBox1.Items.Add("IP地址:"+ip.ToString());
} }
catch
{
}
}
方法2(获得局域网的pc的Name)
ArrayList arr = new ArrayList();
this.listBox1.Items.Clear();
DirectoryEntry root = new DirectoryEntry("WinNT:");
{
foreach(DirectoryEntry domain in root.Children)
{
foreach(DirectoryEntry computer in domain.Children)
{
arr.Add(computer.Name);
this.listBox1.Items.Add(computer.Name+"->"+computer.Path);
}
}
}
获得主机名
private string GetHostname()
{
string m_Hostname = "";
string m_IPstr = this.GetIPAddresses();
IPHostEntry iphost = new IPHostEntry();
iphost = Dns.Resolve(m_IPstr);
m_Hostname = iphost.HostName;
return m_Hostname;
}
获得当前IP
private string GetIPAddresses()
{
string[] addresses = null;
try
{
ArrayList Temp = new ArrayList();
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration ") ;
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
if ((bool)mo["IpEnabled"])
{
string[] ips = (string[])mo["IPAddress"];
foreach (string s in ips)
{
Temp.Add (s);
}
}
}
if (Temp.Count > 0)
{
addresses = new string[Temp.Count];
Temp.CopyTo (addresses);
}
else
{
addresses = new string[0];
}
}
catch (Exception) {} return addresses[0];
}
获得局域网的主机名
你必须先得到局域网内所有的IP地址(或IP地址范围),然后根据IP地址得到主机名。
例如:
string ipMask = "***.***.***.";
for (int i = 128; i<= 255; i++)
{
string ipAddress = ipMask + i.ToString() ;
IPHostEntry iphost = new IPHostEntry();
try
{
iphost = Dns.Resolve(ipAddress); IPAddress[] ipp1 = iphost.AddressList;
foreach(IPAddress ip in ipp1)
{
listBox1.Items.Add("主机名称:"+iphost.HostName );
listBox1.Items.Add("IP地址:"+ip.ToString());
} }
catch
{
}
}
方法2(获得局域网的pc的Name)
ArrayList arr = new ArrayList();
this.listBox1.Items.Clear();
DirectoryEntry root = new DirectoryEntry("WinNT:");
{
foreach(DirectoryEntry domain in root.Children)
{
foreach(DirectoryEntry computer in domain.Children)
{
arr.Add(computer.Name);
this.listBox1.Items.Add(computer.Name+"->"+computer.Path);
}
}
}
获得主机名
private string GetHostname()
{
string m_Hostname = "";
string m_IPstr = this.GetIPAddresses();
IPHostEntry iphost = new IPHostEntry();
iphost = Dns.Resolve(m_IPstr);
m_Hostname = iphost.HostName;
return m_Hostname;
}
获得当前IP
private string GetIPAddresses()
{
string[] addresses = null;
try
{
ArrayList Temp = new ArrayList();
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration ") ;
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
if ((bool)mo["IpEnabled"])
{
string[] ips = (string[])mo["IPAddress"];
foreach (string s in ips)
{
Temp.Add (s);
}
}
}
if (Temp.Count > 0)
{
addresses = new string[Temp.Count];
Temp.CopyTo (addresses);
}
else
{
addresses = new string[0];
}
}
catch (Exception) {} return addresses[0];
}
怎么样使用?
是在什么样的环境下使用?
是在什么样的环境下使用?通过引用System.DirectoryServices.dll
{
//Thread 类: 创建并控制线程
Thread thScan = new Thread(new ThreadStart(ScanTarget));
//Thread.Start 方法:启动线程
thScan.Start();
} private void ScanTarget()
{
//构造IP地址的31-8BIT 位,也就是固定的IP地址的前段
// numericUpDown1是定义的System.Windows.Forms.NumericUpDown控件
string strIPAddress="10.141.60.";
//开始扫描地址
//int nStrat = Int32.Parse(numericUpDown4.Text);
//终止扫描地址
//int nEnd =Int32.Parse(numericUpDown5.Text);
//扫描的操作
for(int i=160;i<=200;i++)
{
string strScanIPAdd = strIPAddress+i.ToString();
//转换成IP地址
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
try
{
//你可以加入自已的,增强功能
// Dns.GetHostByAddress 方法: 根据 IP 地
//址获取 DNS 主机信息。
IPHostEntry myScanHost=Dns.GetHostByAddress(myScanIP);
//获取主机的名
string strHostName =myScanHost.HostName.ToString();
textBox5.AppendText("IP:"+myScanIP.ToString()+" HostName:"+strHostName.ToString()+"\r\n");
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
}