探索IP地址,默认门路,子网掩码 [英] Prob in Finding IPAddress, Default gate way,Subnet mask

查看:109
本文介绍了探索IP地址,默认门路,子网掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi frens





如何使用c#windows窗体查找ipaddress,子网掩码和默认网关?



我尝试了一些代码来找到ipaddress,它返回127.0.0.1这是正确的,但是我手动将ip地址设置为198.168.1.2 ..我怎么能得到它? ?请给出你的建议







提前致谢

Darshan

Hi frens


How can i find out the ipaddress , subnetmask and default gateway using c# windows form ?

I tried out some code to find ipaddress , its returning 127.0.0.1 which is correct , but i ve manually set the ip address to 198.168.1.2 ..How can i get that ?? Please give ur suggestions



Thanks in advance
Darshan

推荐答案

一台机器可以有零个或多个网络接口,IP地址等。你的问题的答案不是单一的答案,而是在每种情况下都有一个列表。



获取每个接口的IP地址和子网掩码:



A machine can have zero or more network interfaces, IP addresses etc. The answer to your question is not a single answer but involves a list in each case.

To get the IP addresses and subnet mask for each interface:

using System;
using System.Net.NetworkInformation;

public class blahblah
{
  public static void Main()
  {
    NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
    foreach(NetworkInterface Interface in Interfaces)
    {
      if(Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
      Console.WriteLine(Interface.Description);
      UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
      foreach(UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
      {
        Console.WriteLine("\tIP Address is {0}", UnicatIPInfo.Address);
        Console.WriteLine("\tSubnet Mask is {0}", UnicatIPInfo.IPv4Mask);
      }
    }
  }
}





打印网关:



To print the gateway:

public void PrintDefaultGateway() 
{ 
   var defaultGateway = 
   from nics in NetworkInterface.GetAllNetworkInterfaces() 
   from props in nics.GetIPProperties().GatewayAddresses 
   where nics.OperationalStatus == OperationalStatus.Up 
   select props.Address.ToString(); 

   Console.WriteLine("\tGateway is {0}", defaultGateway.First());
}


这篇关于探索IP地址,默认门路,子网掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆