首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教怎么获得跟主机同一网段的用户列表阿

2012-01-10 
请问如何获得跟主机同一网段的用户列表阿?就是要获得一个局域网内的IP--主机名列表,请问C#如何实现呢?先谢

请问如何获得跟主机同一网段的用户列表阿?
就是要获得一个局域网内的IP--主机名列表,请问C#如何实现呢?先谢谢了

[解决办法]
多线程扫描网段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace 多线程扫描网段
{
public partial class Form1 : Form
{
private DateTime startTime;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int min = (int)this.numericUpDown4.Value,max = (int)this.numericUpDown5.Value;
if (min> = max) { MessageBox.Show( "网段输入错误! "); return; }
startTime = DateTime.Now;
string mask = this.numericUpDown1.Value.ToString() + ". " + this.numericUpDown2.Value.ToString() + ". " + this.numericUpDown3.Value.ToString();
this.progressBar1.Minimum = min;
this.progressBar1.Maximum = max;
Thread[] threads=new Thread[max-min +1];
while (min <= max)
{
Scan NewThreadScan = new Scan();
NewThreadScan.IP = mask + ". " + min.ToString();
NewThreadScan.GetHostInfo = new HostInfo(newHostInfo);
threads[min - 1] = new Thread(new ThreadStart(NewThreadScan.start));
threads[min - 1].Start();
min++;
}
}
public void newHostInfo(string HostIP, string NewHostName)
{
this.setListBox(HostIP, NewHostName);
}


private void setListBox(string HostIp, string NewHostName)
{
if (listBox1.InvokeRequired && label4.InvokeRequired && progressBar1.InvokeRequired)
{
HostInfo b = new HostInfo(setListBox);
Invoke(b, new object[] { HostIp, NewHostName });
}
else
{
lock (listBox1)
{

listBox1.Items.Add(HostIp + " " + NewHostName);
if (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++;
}
else
{
MessageBox.Show( "成功完成检测! ");
DateTime endTime = DateTime.Now;
TimeSpan timeSp = endTime - startTime;
label4.Text = timeSp.Seconds.ToString() + "秒 ";
progressBar1.Value = progressBar1.Minimum;

}
}
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace 多线程扫描网段
{


class Scan
{
private string _IP;
public string _HostName;
public HostInfo GetHostInfo;
public string IP
{
get { return _IP; }
set { _IP = value; }
}

public Scan()
{

}

public void start()
{
IPAddress tempIp = IPAddress.Parse(_IP);


try
{
IPHostEntry Host = Dns.GetHostByAddress(tempIp);
_HostName = Host.HostName;
}
catch
{
_HostName = "主机没有反映 ";
}

if (this.GetHostInfo!=null)
{
this.GetHostInfo(_IP, _HostName);
}
}


}
}

[解决办法]
DirectoryEntry entryPC = new DirectoryEntry( "WinNT: ");
foreach(DirectoryEntry child in entryPC.Children)
{
TreeNode node = new TreeNode();
node.Text = child.SchemaClassName+ ": "+child.Name;
PCTree.Nodes.Add(node);//PCTree为一个TreeView控件

foreach(DirectoryEntry pc in child.Children)
{
if(String.Compare(pc.SchemaClassName, "computer ",true)==0)
{
TreeNode son = new TreeNode();
//son.Text = pc.Name;
try
{
IPHostEntry hostent = Dns.GetHostByName(pc.Name); // 主机信息
Array addrs = hostent.AddressList; // IP地址数组
IEnumerator it = addrs.GetEnumerator(); // 迭代器
while(it.MoveNext())
{ // 循环到下一个IP 地址
IPAddress ip = (IPAddress)it.Current; // 获得 IP 地址
son.Text=ip.ToString(); // 显示 IP地址
}
}
catch
{
son.Text=pc.Name;
}
node.Nodes.Add(son);
}
}
}
[解决办法]
Public Function GetComputerList() As ArrayList
Dim List As New ArrayList

Dim Root As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry( "WinNT: ")
Dim Domains As System.DirectoryServices.DirectoryEntries = Root.Children

Domains.SchemaFilter.Add( "domain ")

For Each Domain As System.DirectoryServices.DirectoryEntry In Domains
Dim Computers As System.DirectoryServices.DirectoryEntries = Domain.Children
Computers.SchemaFilter.Add( "computer ")

For Each Computer As System.DirectoryServices.DirectoryEntry In Computers
Dim arr(2) As String
Dim iphe As System.Net.IPHostEntry = Nothing

Try
iphe = Dns.GetHostEntry(Computer.Name)
Catch ex As Exception
End Try

arr(0) = Domain.Name : arr(1) = Computer.Name

If iphe IsNot Nothing AndAlso iphe.AddressList.Length > 0 Then
For i As Integer = 0 To iphe.AddressList.Length - 1
arr(2) += iphe.AddressList(i).ToString + ", "
Next

arr(2) = arr(2).ToString().Remove(arr(2).ToString().Length - 1, 1)
Else
arr(2) = " "
End If

List.Add(arr)
Next
Next

'释放资源
Root = Nothing : Domains = Nothing

'返回
Return List
End Function

'遍历法
Public Function ScanComputers(ByVal ipPrefix As String, ByVal StartIP As Integer, ByVal EndIp As Integer) As ArrayList
Dim List As New ArrayList

For i As Integer = StartIP To EndIp
Dim ScanIP As String = ipPrefix + ". " + i.ToString



Try
Dim myScanHost As System.Net.IPHostEntry = Nothing
myScanHost = Dns.GetHostEntry(System.Net.IPAddress.Parse(ScanIP))

If myScanHost IsNot Nothing Then
Dim arr(2) As String
arr(1) = myScanHost.HostName
arr(2) = ScanIP
List.Add(arr)
End If
Catch ex As Exception
End Try
Next

Return List
End Function

热点排行