4

我知道如果 IP 超出子网掩码 + 本地 IP 规则,则只能通过网关访问。问题是我不知道如何使用.NET 以编程方式获取本地IP 地址,也不知道本地子网掩码。你们中的任何人都可以帮助我吗?

我将使用这些信息从我的批处理 SQL 插入队列中获得最大性能。如果 SQL 服务器位于同一子网中,那么它将使用针对最小延迟优化的算法,否则我将使用针对高延迟优化的算法。

4

3 回答 3

10

您可以使用 System.Net.NetworkInformation 命名空间中的类(在 .NET 2.0 中引入):

        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
                    );
            }
        }
于 2009-01-06T14:00:51.227 回答
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();
    }
}

代码示例是 Msdn 提供的示例的混搭形式,简化后仅显示您可能需要的信息。

编辑:我花了太长时间(同时做太多事情:))来发表这篇文章,米奇打败了我:)

于 2009-01-06T14:43:27.350 回答
1

这将为您提供主机名和 IP 地址。我假设您知道 LAN 中的 IP,因此您应该能够确定 IP 地址是否在 LAN 之外:

// 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());
}
于 2009-01-06T13:50:11.763 回答