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.