获取网络中的计算机名??????

大户翁 2003-05-19 01:00:33
在局域网内 如何检测共有多少台计算机,并取回其计算机名 [C#代码如何写???]
急!!!!!! 万分感激!。
...全文
149 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dplxp 2003-05-19
  • 打赏
  • 举报
回复
using System.Net;
using System.IO ;
using System.Threading;
dplxp 2003-05-19
  • 打赏
  • 举报
回复
private void button1_Click(object sender, System.EventArgs e)
{
IPHostEntry myHost = new IPHostEntry();
try
{
// Dns.GetHostName()获取本地计算机的主机名
// Dns.GetHostByName()获取指定 DNS 主机名的 DNS 信息
//得到本地主机的DNS信息
myHost = Dns.GetHostByName(Dns.GetHostName());
//显示本地主机名
textBox1.Text = myHost.HostName.ToString();
//显示本地主机的IP地址表
for(int i=0; i<myHost.AddressList.Length;i++)
{
richTextBox1.AppendText("本地主机IP地址->"+myHost.AddressList[i].ToString()+"\r");
}
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}

}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
IPHostEntry myDnsToIP = new IPHostEntry();
//Dns.Resolve 方法: 将 DNS 主机名或以点分隔的四部分表示法格式的 // IP 地址解析为 IPHostEntry实例
myDnsToIP =Dns.Resolve(textBox2.Text.ToString());
//显示此域名的IP地址的列表
for(int i=0;i<myDnsToIP.AddressList.Length;i++)
{
richTextBox1.AppendText(textBox2.Text+"的IP地址是"+myDnsToIP.AddressList[i].ToString()+"\r");
}
}
catch
{
MessageBox.Show ("没有找到");
}
}

private void button3_Click(object sender, System.EventArgs e)
{
//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=numericUpDown1.Text+"."+numericUpDown2.Text+"."+numericUpDown3.Text+".";
//开始扫描地址
int nStrat = Int32.Parse(numericUpDown4.Text);
//终止扫描地址
int nEnd =Int32.Parse(numericUpDown5.Text);
//扫描的操作
for(int i=nStrat;i<=nEnd;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();
richTextBox1.AppendText(strScanIPAdd+"->"+strHostName+"\r");
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}//for
Knight94 2003-05-19
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topicview.asp?id=1744771
如果觉得慢,可以用线程来做。
大户翁 2003-05-19
  • 打赏
  • 举报
回复
我试过了太慢 是否有别的方法???(速度快)
jcaomao 2003-05-19
  • 打赏
  • 举报
回复
先通过自己的机器名取得取得自己机器的ip

然后再根据自己机器ip取得所有子网机器的有可能ip
如:自己机器192.168.0.9,其他的机器为 192.168.0.1--192.168.0.254

然后用多线程 分别去 gethostname ,如果能try到,就是开着的机器

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧