0

I am making a program that among other things collects system information. However I am having some problems obtaining the subnet mask. I am new to programming so this is probably related to my lack of skills. My code looks like this:

public string Subnet()
{
     string Maske = "";
     foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
     if (f.OperationalStatus == OperationalStatus.Up)
     {
         IPInterfaceProperties ipInterface = f.GetIPProperties();
         foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses)
         {
             Maske = unicastAddress.IPv4Mask.ToString();
         }
     }
     return Maske;
}

I only get 255.0.0.0 as a result. I have disabled or uninstalled the other network cards but I still get this result.

4

1 回答 1

1

您是否知道 ipInterface.UnicastAddresses 或 NetworkInterface.GetAllNetworkInterfaces() 中可能有多个条目?您的方法只采用最后一个找到的方法。我在我的电脑上测试了你的方法,列表包含三个条目:255.255.255.0、0.0.0.0 和 255.0.0.0...

编辑:您可能需要使用 NetworkInterfaceType Loopback 忽略网络接口,因为有时安装了一些虚拟软件接口......

if (f.OperationalStatus == OperationalStatus.Up && f.NetworkInterfaceType != NetworkInterfaceType.Loopback)
于 2014-07-11T06:36:58.230 回答