0

I have a C# function which return Local IP Address.

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;
    }

By using this local IP address, I tried to get MAC Address.

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 "";
        }
    }

Two of the functions work correctly.
But what I would like to do is getting other PC's IP Address at the same LAN network.
Then, If I get those IP Addresses , that will be input value to my

GetMACAddressByIP(string ip)

function.

But my problem is I don't know how to get other pc IP Address.

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

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

        return RemoteIPs;
    }

Then, Next Question is
Is this possible to get MAC Address of PC which is already turn off ?

Every solution will be really appreciated.

Frank Myat Thu
  • 4,448
  • 9
  • 67
  • 113
  • 1
    For your first question, it is a possible duplication of this question at http://stackoverflow.com/questions/1993891/list-the-ip-address-of-all-computers-connected-to-a-single-lan See if it helps. – Tariqulazam Oct 21 '11 at 04:32

5 Answers5

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)
        {

        }
    }
Cory
  • 46
  • 2
2

Method on below is working fine with Windows (tested on WinXP and up) and Linux with Mono (tested on ubuntu, Suse, Redhat).

/// <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();
}
S_R
  • 157
  • 1
  • 4
1

Find machine ip and mac address

cmd > ipconfig/all

//Mac Address [Ethernet adapter Ethernet: > Physical Address]

var macAddress = NetworkInterface.GetAllNetworkInterfaces();
var getTarget = macAddress[0].GetPhysicalAddress();

//IP Address [Ethernet adapter Ethernet: > IPv4 Address]

string myHostName = Dns.GetHostName();
IPHostEntry iphostEntries = Dns.GetHostEntry(myHostName);
IPAddress[] arrIP = iphostEntries.AddressList;
var getIPAddress = arrIP[arrIP.Length - 1].ToString();
mtngunay
  • 69
  • 1
  • 3
0

No, you cannot generally get the MAC address of a PC that is turned off. This is a hardware identifier that is sent in the packet. The only hope you would have - and its a hack, is to check the local systems ARP table, for instance go to a command line and type arp -a

This though is not feasible for what you want to do. In fact even if you knew the IP I believe the technique you have is iffy and definitely won't work in all remote situations (if any)

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
0

One way to find IP addresses of computers in the same LAN is to start pinging all possible IPs. You will get both, IP and MAC address in one shot... from the ones that reply, that is.

Icarus
  • 63,293
  • 14
  • 100
  • 115