Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi I have a requirement that i need to find the computer name,Operating System name,MAC address and network role of a given IP address.I am able to find the Computer name but unable to find MAC address and OS information so please advice me how do i do that thanks in advance.

Regards
Prafulla
Posted
Updated 30-Oct-19 12:33pm

To get IP Address:
C#
private static string GetIpAddress()
       {
           string strHostName = "";
           strHostName = System.Net.Dns.GetHostName();
           IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
           IPAddress[] addr = ipEntry.AddressList;
           return addr[addr.Length - 1].ToString();
       }



To get MAC ID:
C#
private string GetMac()
{
    string Mac = string.Empty;
    ManagementClass MC = new ManagementClass("Win32_NetworkAdapter");
    ManagementObjectCollection MOCol = MC.GetInstances();
    foreach (ManagementObject MO in MOCol)
        if (MO != null)
        {
           if (MO["MacAddress"] != null)
                    {
                         Mac = MO["MACAddress"].ToString();
                         if (Mac != string.Empty)
                             break;
                    }
        }
    return Mac;
}


To get Operating System:
C#
private string GetOSName()
{
    System.OperatingSystem os = System.Environment.OSVersion;
    string osName = "Unknown";


    switch (os.Platform)
    {
        case System.PlatformID.Win32Windows:
            switch (os.Version.Minor)
            {
                case 0:
                    osName = "Windows 95";
                    break;
                case 10:
                    osName = "Windows 98";
                    break;
                case 90:
                    osName = "Windows ME";
                    break;
            }
            break;
        case System.PlatformID.Win32NT:
            switch (os.Version.Major)
            {
                case 3:
                    osName = "Windws NT 3.51";
                    break;
                case 4:
                    osName = "Windows NT 4";
                    break;
                case 5:
                    if (os.Version.Minor == 0)
                        osName = "Windows 2000";
                    else if (os.Version.Minor == 1)
                        osName = "Windows XP";
                    else if (os.Version.Minor == 2)
                        osName = "Windows Server 2003";
                    break;
                case 6:
                    osName = "Windows Vista";
                    break;
            }
            break;
    }

    return osName + ", " + os.Version.ToString();
}

Also refer: OS Name, Version & Product Type[^]

To get Computer Name:
C#
static string GetName ()
{
   string netBiosName = System.Environment.MachineName;

   //return netBiosName;
   // Following method is deprecated
   // string dnsName =
   //     System.Net.Dns.GetHostByName("LocalHost").HostName;

   string dnsName = System.Net.Dns.GetHostName();
   return dnsName;
}

Also refer: Similar discussion[^]

Hope I answered all & it helps!
 
Share this answer
 
v2
Comments
Prafulla Sahu 28-May-12 5:01am    
Thank you very much Prasad for your solution it is useful for me but my requirement is little different,means take one example i have a textbox and we need to enter one IP address of a system among all the system(which are connected in a network(LAN))and based on that IP address we have to find OS information and MAC address will you please help me m stocked any advice will appreciate.

Thank you.
User1454 3-Feb-15 5:57am    
Sir, good explianation....but I need to get the mac address of virtual machine. i.e. based on ip address. This just gives the system mac address. How to get mac address based on IP and also for virtual machines.
Member 11699255 20-Jun-15 8:29am    
in which namespace i find managenmentclass
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900