使用c#查找计算机名称,操作系统名称和给定IP地址的MAC地址 [英] Find Computer Name,Operating System name and MAC address of a given IP address using c#

查看:268
本文介绍了使用c#查找计算机名称,操作系统名称和给定IP地址的MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个要求,我需要找到给定IP地址的计算机名称,操作系统名称,MAC地址和网络角色.我能够找到计算机名称,但无法找到MAC地址和操作系统信息,所以请给我建议我要先谢谢你吗?

问候
Prafulla

解决方案

要获取IP地址:

 私有 静态 字符串 GetIpAddress()
       {
           字符串 strHostName = " ;
           strHostName = System.Net.Dns.GetHostName();
           IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
           IPAddress [] addr = ipEntry.AddressList;
           返回 addr [addr.Length- 1 ].ToString();
       } 




要获取MAC ID:

 私有 字符串 GetMac()
{
    字符串 Mac = 字符串 .Empty;
    ManagementClass MC =  ManagementClass(" );
    ManagementObjectCollection MOCol = MC.GetInstances();
     foreach (ManagementObject MO  in  MOCol)
        如果(MO!= )
        {
           如果(MO [" ] != 为空)
                    {
                         Mac = MO [" ].ToString();
                         如果(Mac!= 字符串 .Empty)
                              break ;
                    }
        }
    返回 Mac;
} 



要获取操作系统:

 私有 字符串 GetOSName()
{
    System.OperatingSystem os = System.Environment.OSVersion;
    字符串 osName = " ;


    开关(操作系统平台)
    {
        案例 System.PlatformID.Win32Windows:
            开关(os.Version.Minor)
            {
                案例  0 :
                    osName = " ;
                     break ;
                案例  10 :
                    osName = " ;
                     break ;
                案例  90 :
                    osName = " ;
                     break ;
            }
             break ;
        案例 System.PlatformID.Win32NT:
            开关(操作系统版本主要)
            {
                案例  3 :
                    osName = " ;
                     break ;
                案例  4 :
                    osName = " ;
                     break ;
                案例  5 :
                    如果(os.Version.Minor ==  0 )
                        osName = " ;
                    其他 如果(os.Version.Minor ==  1 )
                        osName = " ;
                    其他 如果(os.Version.Minor ==  2 )
                        osName = " ;
                     break ;
                案例  6 :
                    osName = " ;
                     break ;
            }
             break ;
    }

    返回 osName + "  + os .Version.ToString();
} 


另请参阅: OS名称,版本&产品类型 [^ ]

获取计算机名称:

 静态 字符串 GetName()
{
   字符串 netBiosName = System.Environment.MachineName;

   // 返回netBiosName; 
   // 不推荐使用以下方法
   // 字符串dnsName = 
   //  System.Net.Dns.GetHostByName("LocalHost").HostName; 

   字符串 dnsName = System.Net.Dns.GetHostName();
   返回 dnsName;
} 


另请参阅:类似的讨论 [ ^ ]

希望我能回答所有&


使用c# [ ^ ]


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

解决方案

To get IP Address:

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:

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:

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:

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!


get MAC address in message box using c#[^]


这篇关于使用c#查找计算机名称,操作系统名称和给定IP地址的MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆