0

我必须开发一个桌面应用程序,它允许我在本地网络上共享文件。为此,我可以获得一个listview设备主机名,但是当我知道他们的 IP 地址和 MAC 时,我去查看System.net了其他几个 MSDN 论坛(地铁应用程序的帮助可用)。

如何获取本地网络上所有设备的 IP 地址和 MAC 地址?

DirectoryEntry用来获取用户名并显示在listview.

   lstLocal.Items.Clear();
        lstLocal.View = View.Details;
        lstLocal.FullRowSelect = true;

        DirectoryEntry root = new DirectoryEntry("WinNT:");

        foreach (DirectoryEntry computers in root.Children)
        {
            foreach (DirectoryEntry computer in computers.Children)
            {
                if (computer.Name != "Schema")
                {
                    ListViewItem item = new ListViewItem(computer.Name);
                   // item.SubItems.Add(computer.Name);

                    //MessageBox.Show(computer.Name);
                    lstLocal.Items.Add(item);

                }

            }
        }

更新:我用过

                     var hostname = Dns.GetHostName();
                    var ipadd = Dns.GetHostAddresses(hostname);

但地址是在 IPV6 中返回的。我需要它在 IPV4 中。

4

4 回答 4

1

如果您已经有了主机名,那么您就差不多完成了。

你可以

于 2014-12-16T08:20:29.187 回答
0

你可以试试这个:

对于 IP 地址:

var hostname = Dns.GetHostName();
var ipadd = Dns.GetHostAddresses(hostname); //array will contain ipv4 and ipv6

对于 MAC 地址:

    public string GetMACAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
            if (sMacAddress == String.Empty)// only return MAC Address from first card  
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                sMacAddress = adapter.GetPhysicalAddress().ToString();
            }
        } return sMacAddress;
    }

MAC地址参考:

MAC地址

于 2014-12-16T09:52:42.637 回答
0

有内置类可以查找 IP/Mac 地址。请检查此链接。

从上面截取的片段:

System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
                foreach(IPAddress TempA in TempAd)
                {
                    Ipaddr[1] = TempA.ToString();

                    byte[] ab = new byte[6];
                    int len = ab.Length;

                    // This Function Used to Get The Physical Address
                    int r = SendARP( (int) TempA.Address, 0, ab, ref len );
                    string mac = BitConverter.ToString( ab, 0, 6 );

                    Ipaddr[2] = mac;
                }           
于 2014-12-16T08:29:04.600 回答
0

使用WMI mac 地址可以检测到“机器”是你的机器名。在项目中添加对 System.Management 的引用

ManagementScope scope = new ManagementScope();
var options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.Default;
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;

scope = new ManagementScope(@"\\" + machine + "\\root\\CIMV2", options);
scope.Connect();
SelectQuery query = new SelectQuery("Select * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher objMOS = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objMOC = objMOS.Get();
string macAddress = String.Empty;
foreach (ManagementObject objMO in objMOC) {
    object tempMacAddrObj = objMO["MacAddress"];

    if (tempMacAddrObj == null) {
        continue;
    }
    if (macAddress == String.Empty) {
        macAddress = tempMacAddrObj.ToString();
    }
    objMO.Dispose();
}

return macAddress;
于 2016-05-09T06:31:55.680 回答