see this:
http://expert.csdn.net/Expert/topic/2031/2031013.xml?temp=.8035547

解决方案 »

  1.   

    using System;
    using System.Net;
    using System.Net.Sockets;
     
    class GetIP
    {
        public static void Main(string[] args)
        {
            if(args.Length != 1)
            {
                usage();
                return;
            }        try
            {
                // 得到主机的信息,放到IPHostEntry实例中
                IPHostEntry IPHost = Dns.Resolve(args[0]);
                Console.WriteLine("HostName: " + IPHost.HostName);
                
                // 从IPHostEntry实例中提取关于主机的信息并打印出来
                string []aliases = IPHost.Aliases;
                Console.WriteLine("Count of host aliases: " + aliases.Length);
                for(int i= 0; i < aliases.Length ; i++)
                {
                    Console.WriteLine(aliases[i]);
                }
     
                IPAddress[] addr = IPHost.AddressList;
                Console.WriteLine("Count of host addresses: " + addr.Length);
                Console.WriteLine("Host IP list: ");
                for(int i= 0; i < addr.Length ; i++)
                {
                    Console.WriteLine(addr[i]);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString()) ;
            }
        }     private static void usage()
        {
            Console.WriteLine("Usage: getIP hostname");
        }
    }
      

  2.   

    GetHostByName 方法使用这个方法
      

  3.   

    有三个方法,通过Dns,上面的已经讲了
    还有就是通过注册表和wmI
    注册表:
    using System;
    using Microsoft.Win32;
    class CardGrab
    {
      public static void Main ()
      {
      RegistryKey start = Registry.LocalMachine;
      RegistryKey cardServiceName, networkKey;
      string networkcardKey = "SOFTWARE\\Microsoft\\ &Acirc;
          Windows NT\\CurrentVersion\\NetworkCards";
      string serviceKey =
          "SYSTEM\\CurrentControlSet\\Services\\";
      string networkcardKeyName, deviceName;
      string deviceServiceName, serviceName;
      RegistryKey serviceNames =
                start.OpenSubKey(networkcardKey);
      if (serviceNames == null)
      {
        Console.WriteLine("Bad registry key");
        return;
      }
      string[] networkCards = serviceNames.GetSubKeyNames();
      serviceNames.Close();
      foreach(string keyName in networkCards)
      {
        networkcardKeyName = networkcardKey + "\\" + keyName;
        cardServiceName = start.OpenSubKey(networkcardKeyName);
        if (cardServiceName == null)
        {
         Console.WriteLine("Bad registry key: {0}",
             networkcardKeyName);
         return;
        }
        deviceServiceName =
           (string)cardServiceName.GetValue("ServiceName");
              deviceName =
               (string)cardServiceName.GetValue("Description");
        Console.WriteLine("\nNetwork card: {0}", deviceName);
         
        serviceName = serviceKey + deviceServiceName +
          "\\Parameters\\Tcpip";
        networkKey = start.OpenSubKey(serviceName);
        if (networkKey == null)
        {
         Console.WriteLine("  No IP configuration set");
        } else
        {
          string[] ipaddresses =
            (string[])networkKey.GetValue("IPAddress");
          string[] defaultGateways =
            (string[])networkKey.GetValue("DefaultGateway");
          string[] subnetmasks =
            (string[])networkKey.GetValue("SubnetMask");
          foreach(string ipaddress in ipaddresses)
          {
            Console.WriteLine("  IP Address: {0}",ipaddress);
          }
          foreach(string subnetmask in subnetmasks)
          {
            Console.WriteLine("  Subnet Mask: {0}",
             subnetmask);
          }
          foreach(string defaultGateway in defaultGateways)
          {
            Console.WriteLine("  Gateway: {0}",
                  defaultGateway);
          }
          networkKey.Close();
        }
      }
      start.Close();
      }
    }
      

  4.   

    Using wmi:
    using System;
    using System.Management;
    class WMICardGrab
    {
      public static void Main ()
      {
        ManagementObjectSearcher query = new
        ManagementObjectSearcher("SELECT * FROM &Acirc;
         Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
        ManagementObjectCollection queryCollection = query.Get();
        foreach( ManagementObject mo in queryCollection )
        {
          string[] addresses = (string[])mo["IPAddress"];
          string[] subnets = (string[])mo["IPSubnet"];
          string[] defaultgateways =
               (string[])mo["DefaultIPGateway"];
          Console.WriteLine("Network Card: {0}",
                 mo["Description"]);
          Console.WriteLine("  MAC Address: {0}",
                 mo["MACAddress"]);
          foreach(string ipaddress in addresses)
          {
            Console.WriteLine("  IP Address: {0}",
                 ipaddress);
          }
          foreach(string subnet in subnets)
          {
            Console.WriteLine("  Subnet Mask: {0}", subnet);
          }
          foreach(string defaultgateway in defaultgateways)
          {
            Console.WriteLine("  Gateway: {0}",
                 defaultgateway);
          }
        }
      }
    }
      

  5.   

    using System;
    using System.Net;
    namespace NKUtilities 
    {
        public class DNSUtility
        {
    public static int Main (string [] args)
    {

    string strHostName = "";
    if (args.Length == 0)
    {
    // 获得本地的IP地址
    //首先获了本地机的主机名
    strHostName = Dns.GetHostName ();
    Console.WriteLine ("Local Machine's Host Name: " + strHostName);
    Console.Read();
    }
    else
    {
    strHostName = args[0];
    } // 接着使用主机名,获取IP地址列表

    IPHostEntry ipEntry = Dns.GetHostByName (strHostName);
    IPAddress [] addr = ipEntry.AddressList; for (int i = 0; i < addr.Length; i++)
    {
    Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
    Console.Read();
    }
    return 0;

    }
    }
      

  6.   

    不好用,干脆直接调用ipconfig.exe算了
      

  7.   

    一个控制台程序:
    using System.Net;-------------------------------------(别忘了)
    string strHostName = Dns.GetHostName ();
    Console.WriteLine(strHostName);IPHostEntry ipEntry = Dns.GetHostByName (strHostName);
    IPAddress [] IpAddr = ipEntry.AddressList;
    for (int i = 0; i < IpAddr.Length; i++)
    {
       Console.WriteLine ("IP Address {0}: {1} ", i, IpAddr[i].ToString ());
    }
    试试吧!