[c#] Mac Address/ Ip Address 가져오기

2012. 8. 9. 00:00프로그램개발/c#

아래 예제 참조 하세요.

간단합니다. ㅎㅎ

 

public static String getMacAddress(String ipaddress)
{
    String queryStr = "select * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'";
    System.Management.ObjectQuery objectQuery = new System.Management.ObjectQuery(queryStr);
    System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(objectQuery);
    System.Management.ManagementObjectCollection mos = searcher.Get();
    String macAddress = null;

    foreach (System.Management.ManagementObject mo in mos)
    {
 if (Utils.isEmpty(ipaddress))
 {
     macAddress = mo["MACAddress"].ToString();
     break;
 }
 else
 {
     String[] address = (String[])mo["IPAddress"];
     if (ipaddress.Equals(address[0]))
     {
  macAddress = mo["MACAddress"].ToString();
  break;
     }
 }
    }

    return macAddress;
}

public static String getIpAddress(String macaddress)
{
    String queryStr = "select * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'";
    System.Management.ObjectQuery objectQuery = new System.Management.ObjectQuery(queryStr);
    System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(objectQuery);
    System.Management.ManagementObjectCollection mos = searcher.Get();
    String IpAddress = null;

    foreach (System.Management.ManagementObject mo in mos)
    {
 if (Utils.isEmpty(macaddress))
 {
     String[] address = (String[])mo["IPAddress"];
     IpAddress = address[0];
     break;
 }
 else
 {
     String mac = mo["MACAddress"].ToString();
     if (macaddress.Equals(mac))
     {
  String[] address = (String[])mo["IPAddress"];
  IpAddress = address[0];
  break;
     }
 }
    }

    return IpAddress;
}

반응형