下面的5行代码完成了从局域网中找出所有的机器
foreach(DirectoryEntry domain in root.Children)
{
lb_pcName.Items.Add("★★★"+domain.Name+"★★★");
foreach(DirectoryEntry pc in domain.Children)
{
if(pc.Name!="Schema")//Schema是结束标记
lb_pcName.Items.Add(" "+pc.Name);
}
}
foreach(DirectoryEntry domain in root.Children)
{
lb_pcName.Items.Add("★★★"+domain.Name+"★★★");
foreach(DirectoryEntry pc in domain.Children)
{
if(pc.Name!="Schema")//Schema是结束标记
lb_pcName.Items.Add(" "+pc.Name);
}
}
获取用户信息:Page.User
获取客户端电脑名:Page.Request.UserHostName
获取客户端电脑IP:Page.Request.UserHostAddress2. 在网络编程中的通用方法:
获取当前电脑名:static System.Net.Dns.GetHostName()
根据电脑名取出全部IP地址:static System.Net.Dns.Resolve(电脑名).AddressList
也可根据IP地址取出电脑名:static System.Net.Dns.Resolve(IP地址).HostName3. 系统环境类的通用属性:
当前电脑名:static System.Environment.MachineName
当前电脑所属网域:static System.Environment.UserDomainName
当前电脑用户:static System.Environment.UserName
IPHostEntry hostInfo = Dns.GetHostByName(Dns.GetHostName());
IPAddress[] address = hostInfo.AddressList;
string m_IP= address[0].ToString();
int i=m_IP.LastIndexOf(".");
string a=m_IP.Substring(0,i+1);
for(i=110;i<256;i++)
{
string ip=a+i.ToString();
try
{
IPHostEntry m_HostName= System.Net.Dns.GetHostByAddress(ip);
Console.WriteLine(m_HostName.HostName);
}
catch
{
}
}
public class class_getMAC
{ //调用WIN API
[DllImport("Iphlpapi.dll")]
public static extern int SendARP (Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);//dest为目标机器的IP;Host为本机器的IP [DllImport("Ws2_32.dll")]
public static extern Int32 inet_addr (string ip); //发布事件
public event Return_MAC return_MAC;
/////////////
private string []ip;
private Int32 host_ip; //构造函数
public class_getMAC(string []a,Int32 myhostip)
{
this.ip=a;
this.host_ip=myhostip; } public void RUN()
{
dowork(this.ip); } public void dowork(string [] str_ip)
{
foreach(string s in str_ip)
dowork(s); } public void dowork(string str_ip)
{
string name="";
Int64 macinfo=new Int64();
Int32 len=6;
Int32 mysource=this.host_ip; //转换IP地址
IPAddress myscanip=IPAddress.Parse(str_ip);
System.Windows.Forms.Application.DoEvents();
IPHostEntry myscanhost=null;
string a,bb,c;
a=str_ip;
bb="";
c="";
try
{
myscanhost=Dns.GetHostByAddress(myscanip);
Int32 iptest=inet_addr(myscanip.ToString());//目标IP类型转换
//获得机器名称
name=myscanhost.HostName.ToString();
SendARP(iptest,mysource,ref macinfo,ref len); string z;
z=System.Convert.ToString(macinfo,16);
//把MAC地址转换,头尾顺序调换
string[] b=new string[6];
string box="";
int l=12;
for(int i=0;i<6;i++)
{
b[i]=(z.Substring(l-2,2)).ToUpper();//大写
l-=2; }
box=b[0];
for(int j=1;j<6;j++)
{
box=box+"-"+b[j];
}
bb=name;
c=box;
} catch(Exception my)
{
bb="";
c="";
}
finally
{
if(bb!="" && c!="")
{
return_MAC(a,bb,c);
}
}
}
}
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.IContainer components; private Process []myp=null;
//委托
private delegate void updatelist(string a,string b,string c);
private delegate void updatelable(int i);
private int count;
private Thread [] mythread=null;
private int threadcount; //调用WIN API
[DllImport("Ws2_32.dll")]
public static extern Int32 inet_addr (string ip);
.......
.............................
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); this.label1.Visible=false;
ColumnHeader a=new ColumnHeader();
ColumnHeader b=new ColumnHeader();
ColumnHeader c=new ColumnHeader();
a.Text="计算机的IP地址";
a.Width=150;
a.TextAlign=System.Windows.Forms.HorizontalAlignment.Center;
b.Text="计算机的名称";
b.Width=100;
b.TextAlign=System.Windows.Forms.HorizontalAlignment.Center; c.Text="计算机的MAC地址";
c.Width=180;
c.TextAlign=System.Windows.Forms.HorizontalAlignment.Center; this.listView1.Columns.Add(a);
this.listView1.Columns.Add(b);
this.listView1.Columns.Add(c);
}
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.button1.Visible =false;
//本机IP
string ips;
IPHostEntry host_info=Dns.GetHostByName(System.Environment.MachineName);
ips=host_info.AddressList[0].ToString();
Int32 myhost_ip=inet_addr(ips);//本地IP
string ip_head;
ip_head=ips.Substring(0,ips.LastIndexOf(".")+1); this.label1.Visible=true;
int start=1,end=255;
count=end-start;
//获取要扫描的IP
ArrayList my=new ArrayList();
string kk="";
for(int i=start;i<=end;i++)
{
kk=ip_head+i.ToString();
my.Add(kk.Trim());
} string [] scan_ip=(string [])my.ToArray(typeof(string)); /////
///
int dump=5;
threadcount=51;
mythread=new Thread[threadcount];
if(scan_ip.Length>0)
{
//多线程
for(int k=0;k<threadcount;k++)
{
string []a=new string[dump];
Array.Copy(scan_ip,k*dump,a,0,dump);
System.Windows.Forms.Application.DoEvents(); class_getMAC my_MAC=new class_getMAC(a,myhost_ip);
mythread[k]=new Thread(new ThreadStart(my_MAC.RUN));
//注册事件
my_MAC.return_MAC +=new Return_MAC(this.update_listview);
//启动线程
mythread[k].Start();
}
//count=myp.Threads.Count;
}
myp=Process.GetProcessesByName("GET_MAC");
MessageBox.Show(myp[0].Threads.Count.ToString());
count=myp[0].Threads.Count;
} private void Form1_Load(object sender, System.EventArgs e)
{
} private void Form1_SizeChanged(object sender, System.EventArgs e)
{
if(this.WindowState==System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
} private void notifyIcon1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
} private void notifyIcon1_Click(object sender, System.EventArgs e)
{
this.Visible=true;
this.WindowState=System.Windows.Forms.FormWindowState.Normal;
this.notifyIcon1.Visible=false;
} private void update_listview(string a,string b,string c)
{
//要采用异步调用
this.listView1.BeginInvoke( new updatelist(dowork),new object []{a,b,c});
}
private void dowork(string a,string b,string c)
{
string []s=new string[3];
s[0]=a;s[1]=b;s[2]=c;
this.listView1.Items.Add(new ListViewItem(s));
this.listView1.Refresh(); System.Windows.Forms.Application.DoEvents();
//异步更新
this.label1.BeginInvoke(new updatelable(dowork1),new object[]{this.listView1.Items.Count});
}
private void dowork1(int i)
{ this.label1.Text=" 正在搜索,已搜索了"+ i.ToString()+"台机器";
if(count<5)
/ this.label1.Text=" 搜索完毕!!"; }
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
myp[0].Kill();
} private void Form1_Activated(object sender, System.EventArgs e)
{
Application.EnableVisualStyles();
this.button1.FlatStyle=System.Windows.Forms.FlatStyle.System;
} private void label1_Click(object sender, System.EventArgs e)
{
}
}