2009-01-06 26 views
4

我知道,如果IP位于子网掩码+本地IP规则之外,它将只能通过网关访问。问题是我不知道如何获取本地IP地址,本地子网掩码,编程式使用.NET。 你们中的任何一个人都可以帮助我?如何确定IP是否在.NET中以编程方式来自同一局域网C#

我将使用此信息从我的批量SQL插入队列中挤出最大性能。如果SQL服务器属于同一个子网,那么它将使用针对最小延迟优化的算法,否则我将使用针对高延迟优化的算法。

回答

10

可以使用System.Net.NetworkInformation命名空间中的类(

 NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); 

     foreach (NetworkInterface iface in interfaces) 
     { 
      IPInterfaceProperties properties = iface.GetIPProperties(); 

      foreach (UnicastIPAddressInformation address in properties.UnicastAddresses) 
      { 
       Console.WriteLine(
        "{0} (Mask: {1})", 
        address.Address, 
        address.IPv4Mask 
        ); 
      } 
     } 
1

这会得到你的主机名和IP地址。我假设你知道在你的局域网IP地址的,所以你应该能够确定该IP地址是局域网之外的方式:

// Get the host name of local machine. 
strHostName = Dns.GetHostName(); 
Console.WriteLine("Local Machine's Host Name: " + strHostName); 

// Using the host name, get the IP address list. 
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); 
IPAddress[] addr = ipEntry.AddressList; 

for (int i = 0; i < addr.Length; i++) 
{ 
    Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString()); 
} 
4

有使用NetworkInformation类的另一种方法:

public static void ShowNetworkInterfaces() 
{ 
    // IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); 
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 

    if (nics == null || nics.Length < 1) 
    { 
     Console.WriteLine(" No network interfaces found."); 
     return; 
    } 

    Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length); 
    foreach (NetworkInterface adapter in nics) 
    { 
     IPInterfaceProperties properties = adapter.GetIPProperties(); 
     Console.WriteLine(); 
     Console.WriteLine(adapter.Description); 
     Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'=')); 
     Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType); 
     Console.WriteLine(" Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString()); 
     string versions =""; 

     // Create a display string for the supported IP versions. 
     if (adapter.Supports(NetworkInterfaceComponent.IPv4)) 
     { 
      versions = "IPv4"; 
     } 
     if (adapter.Supports(NetworkInterfaceComponent.IPv6)) 
     { 
      if (versions.Length > 0) 
      { 
       versions += " "; 
      } 
      versions += "IPv6"; 
     } 
     Console.WriteLine(" IP version .............................. : {0}", versions); 
     UnicastIPAddressInformationCollection uniCast = properties.UnicastAddresses; 
     if (uniCast != null) 
     { 
      foreach (UnicastIPAddressInformation uni in uniCast) 
      { 
       Console.WriteLine(" Unicast Address ......................... : {0}", uni.Address); 
       Console.WriteLine("  Subnet Mask ......................... : {0}", uni.IPv4Mask); 
      } 
     } 
    Console.WriteLine(); 
    } 
} 

代码样品混搭形成前在.NET 2.0)引入由Msdn提供的简单,只显示您可能需要的信息。

编辑:带我太久了(太多东西在同一时间:))使这个职位,米奇击败了我:)

相关问题