| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 356 人关注过本帖
标题:急 帮帮忙!!!!!!!!!!!!!
只看楼主 加入收藏
不悔人
Rank: 2
等 级:论坛游民
帖 子:37
专家分:37
注 册:2009-10-21
结帖率:80%
收藏
 问题点数:0 回复次数:3 
急 帮帮忙!!!!!!!!!!!!!
我想写个程序便利一下局域网中的所有IP地址
2009-10-22 17:10
saitor
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:5
帖 子:373
专家分:1520
注 册:2009-5-18
收藏
得分:0 
google 搜下啊
Dns.GetHostAddresses(Dns.GetHostName())[0].ToString () ;
2009-10-22 17:16
不悔人
Rank: 2
等 级:论坛游民
帖 子:37
专家分:37
注 册:2009-10-21
收藏
得分:0 
那只是显示本机的IP地址  我试过了好多  遍历应该是

foreach(IPAddress ip in me.AddressList)
            {
 
                listBox1.Items.Add(ip);
            
            }

这些都是本机的  不行啊  我搜过了  没有讲这个的   有好多都过时了



大哥们帮帮忙啊
2009-10-22 17:26
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
试试这个:
程序代码:
using System;
using System.Collections.Generic;
using  System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using   System.DirectoryServices; 

namespace WindowsApplication27
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
        [DllImport("Ws2_32.dll")]

        private static extern Int32 inet_addr(string ip);
        private string macAddress, ipAddress;
        private bool canPing = false;

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            DirectoryEntry root = new DirectoryEntry("WinNT:");
            DirectoryEntries domains = root.Children;
            domains.SchemaFilter.Add("domain");

            foreach (DirectoryEntry domain in domains)
            {
                //this.listView1.Items.Add(domain.Name.ToString());
                DirectoryEntries computers = domain.Children;
                computers.SchemaFilter.Add("computer");

                foreach (DirectoryEntry computer in computers)
                {
                    this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[i].Cells[0].Value = i + 1;

                    this.dataGridView1.Rows[i].Cells[1].Value = computer.Name.ToString();
                    IPHostEntry iphe = null;
                    try
                    {
                        iphe = Dns.GetHostEntry(computer.Name.ToString());
                        this.dataGridView1.Rows[i].Cells[2].Value = iphe.AddressList[0].ToString();
                        ipAddress = iphe.AddressList[0].ToString();
                        this.dataGridView1.Rows[i].Cells[3].Value = GetMacAddress();

                    }
                    catch
                    {
                        continue;
                    }
                    this.dataGridView1.Rows[i].Cells[4].Value = domain.Name.ToString();
                    i++;
                }
            }
        }

        private string  GetMacAddress() // 得到指定IP的MAC地址 
        { 
            Int32 ldest = 0; 
            try 
            { 
                ldest = inet_addr(ipAddress); 
            } 
            catch (Exception iperr) 
            { 
                MessageBox.Show(iperr.Message); 
            } 
            Int64 macinfo = new Int64(); 
            Int32 len = 6; 
            try 
            { 
                int res = SendARP(ldest, 0, ref macinfo, ref len); 
            } 
            catch (Exception err) 
            { 
                //    throw new Exception("在解析MAC地址过程发生了错误!"); 
                MessageBox.Show(err.Message); 
            } 
            string originalMACAddress = macinfo.ToString("X4"); 
            if (originalMACAddress != "0000" && originalMACAddress.Length == 12) 
            { //合法MAC地址 
                string mac1, mac2, mac3, mac4, mac5, mac6; 
                mac1 = originalMACAddress.Substring(10, 2); 
                mac2 = originalMACAddress.Substring(8, 2); 
                mac3 = originalMACAddress.Substring(6, 2); 
                mac4 = originalMACAddress.Substring(4, 2); 
                mac5 = originalMACAddress.Substring(2, 2); 
                mac6 = originalMACAddress.Substring(0, 2); 
                macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6; 
                canPing = true; 
            } 
            else 
            { 
                macAddress = "无法探测到MAC地址"; 
                canPing = false; 
            } 
            return macAddress; 
        } 

    }
}
界面上的元素看截图;
未命名22.rar (7.83 KB)



[ 本帖最后由 jedypjd 于 2009-10-23 08:48 编辑 ]

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-10-22 19:48
快速回复:急 帮帮忙!!!!!!!!!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021972 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved