2011-10-21 369 views
0

我有一個C#函數返回本地IP地址。MAC地址和IP地址

private string GetLocalIPByHostName() 
    { 
     string host = Dns.GetHostName(); 
     string LocalIP = string.Empty; 
     IPHostEntry ip = Dns.GetHostEntry(host); 
     foreach (IPAddress _IPAddress in ip.AddressList) 
     { 
      if (_IPAddress.AddressFamily.ToString() == "InterNetwork") 
      { 
       LocalIP = _IPAddress.ToString(); 

      } 
     } 
     return LocalIP; 
    } 

通過使用此本地IP地址,我試圖獲得MAC地址。

protected string GetMACAddressByIP(string ip) 
    { 
     try 
     { 
      ManagementObjectSearcher query= new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration"); 
      ManagementObjectCollection queryCollection = query.Get(); 
      bool Found = false; 
      foreach(ManagementObject _ManagementObject in queryCollection) 
      { 
       if (_ManagementObject["IPAddress"] != null) 
       { 
        string _IPAddress; 
        _IPAddress = string.Join(".", (string[])_ManagementObject["IPAddress"]); 

        if(!_IPAddress.Equals("")) 
        { 
         if(_IPAddress.Equals(ip.Trim())) 
         { 
           Found = true; 
         } 
        } 

        if(Found == true) 
        { 
         if (_ManagementObject["macaddress"] != null) 
         { 
          if (!_ManagementObject["macaddress"].Equals("")) 
          { 
           return (string)_ManagementObject["macaddress"]; 
          } 
         } 
        } 
        else 
        { 
         Found = false; 
        } 
       } 
      } 

      MessageBox.Show("No Mac Address Found"); 
      return ""; 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      return ""; 
     } 
    } 

其中兩個功能正常工作。
但是我想要做的是在同一個LAN網絡上獲取其他PC的IP地址。
然後,如果我得到的IP地址,這將是輸入值我

GetMACAddressByIP(string ip) 

功能。

但我的問題是我不知道如何獲得其他電腦的IP地址。

private List<string> GetRemoteIPs(string LocalIPAddress) 
    { 
     List<string> RemoteIPs = new List<string>(); 

      /*** Here code will be as suggestion of yours. ****/  

     return RemoteIPs; 
    } 

然後,接下來的問題是
這是可能獲得PC這已經是關閉的MAC地址?

每個解決方案都將非常感激。

+1

關於第一個問題,就是這個問題在 一個可能的重複http://stackoverflow.com/questions/1993891/list-the-ip-address-of-all-computers-connected-to -a-single-lan 看看是否有幫助。 – Tariqulazam

回答

3
// Get all active IP connections on the network 
    private void btnSearch_Click(object sender, EventArgs e) 
    { 
     System.Net.NetworkInformation.IPGlobalProperties network = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); 
     System.Net.NetworkInformation.TcpConnectionInformation[] connections = network.GetActiveTcpConnections(); 

     foreach (System.Net.NetworkInformation.TcpConnectionInformation connection in connections) 
     { 

     } 
    } 
+1

歡迎來到計算器!提供示例代碼的簡短說明總是更好,以提高發布準確性:) –

0

不,您通常無法獲取已關閉PC的MAC地址。這是在數據包中發送的硬件標識符。唯一的希望 - 以及它的黑客,是檢查本地系統ARP表,例如去命令行並輸入arp -a

這雖然不適合你想要做的事情。事實上,即使你知道IP,我相信你所擁有的技術是有限的,絕對不會在所有的遠程情況下工作(如果有的話)

0

找到同一局域網中計算機的IP地址的一種方法是開始ping所有可能的IP。你可以同時獲得IP地址和MAC地址,也就是說,只需一次回覆即可。下面是工作正常的Windows(測試在WinXP和起來)和Linux使用Mono(ubuntu上測試,SUSE紅帽

1

方法。

/// <summary>Get Network Interface Addresses information of current machine.</summary> 
/// <returns>Returns Array of Tuple of Mac Address, IP Address, and Status.</returns> 
public virtual Tuple<string, IPAddress, OperationalStatus>[] GetNetworkInterfaceAddresses() 
{ 
    List<Tuple<string, IPAddress, OperationalStatus>> list = new List<Tuple<string, IPAddress, OperationalStatus>>(); 

    NetworkInterfaceType[] acceptedNetInterfaceTypes = new NetworkInterfaceType[] 
      { 
       NetworkInterfaceType.Ethernet, 
       NetworkInterfaceType.Ethernet3Megabit, 
       NetworkInterfaceType.FastEthernetFx, 
       NetworkInterfaceType.FastEthernetT, 
       NetworkInterfaceType.GigabitEthernet, 
       NetworkInterfaceType.Wireless80211 
      }; 

    List<NetworkInterface> adapters = NetworkInterface.GetAllNetworkInterfaces() 
     .Where(ni => acceptedNetInterfaceTypes.Contains(ni.NetworkInterfaceType)).ToList(); 

    #region Get the Mac Address 

    Func<NetworkInterface, string> getPhysicalAddress = delegate(NetworkInterface am_adapter) 
    { 
     PhysicalAddress am_physicalAddress = am_adapter.GetPhysicalAddress(); 
     return String.Join(":", am_physicalAddress.GetAddressBytes() 
      .Select(delegate(byte am_v) 
      { 
       string am_return = am_v.ToString("X"); 
       if (am_return.Length == 1) 
       { 
        am_return = "0" + am_return; 
       } 

       return am_return; 
      }).ToArray()); 
    }; 

    #endregion 

    #region Get the IP Address 

    Func<NetworkInterface, IPAddress> getIPAddress = delegate(NetworkInterface am_adapter) 
    { 
     IPInterfaceProperties am_ipInterfaceProperties = am_adapter.GetIPProperties(); 
     UnicastIPAddressInformation am_unicastAddressIP = am_ipInterfaceProperties.UnicastAddresses 
      .FirstOrDefault(ua => ua.Address != null && ua.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); 

     if (am_unicastAddressIP == null) 
     { 
      return null; 
     } 

     return am_unicastAddressIP.Address; 
    }; 

    #endregion 

    // It's possible to have multiple UP Network Interface adapters. So, take the first order from detected Network Interface adapters. 
    NetworkInterface firstOrderActiveAdapter = adapters.FirstOrDefault(ni => ni.OperationalStatus == OperationalStatus.Up); 

    string macAddress; 
    IPAddress ipAddress; 

    if (firstOrderActiveAdapter != null) 
    { 
     macAddress = getPhysicalAddress(firstOrderActiveAdapter); 
     ipAddress = getIPAddress(firstOrderActiveAdapter); 
     if (ipAddress == null) 
     { 
      throw new Exception("Unable to get the IP Address v4 from first order of Network Interface adapter of current machine."); 
     } 

     list.Add(new Tuple<string, IPAddress, OperationalStatus>(macAddress, ipAddress, firstOrderActiveAdapter.OperationalStatus)); 
     adapters.Remove(firstOrderActiveAdapter); 
    } 

    foreach (NetworkInterface adapter in adapters) 
    { 
     macAddress = getPhysicalAddress(adapter); 
     ipAddress = getIPAddress(adapter); 
     list.Add(new Tuple<string, IPAddress, OperationalStatus>(macAddress, ipAddress, adapter.OperationalStatus)); 
    } 

    if (firstOrderActiveAdapter == null) 
    { 
     throw new Exception("Unable to get the Active Network Interface of the current machine."); 
    } 

    return list.ToArray(); 
}