功能:获取本机电脑第一块启用网卡上的DNS地址,如果设置两个dns,就显示2个,若只设置了一个dns,就显示一个dns地址,
功能结果演示:
完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;
namespace 获取本机dns
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static class NetworkClass
{
public static string GetDnsinfo()
{
string text = "本机DNS服务器:";
NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
int num = 0;
NetworkInterface[] array = allNetworkInterfaces;
int num2 = 0;
if (num2 < array.Length)
{
NetworkInterface networkInterface = array[num2];
if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
num++;
IPInterfaceProperties iPProperties = networkInterface.GetIPProperties();
IPAddressCollection dnsAddresses = iPProperties.DnsAddresses;
if (dnsAddresses.Count > 0)
{
using (IEnumerator<IPAddress> enumerator = dnsAddresses.GetEnumerator())
{
while (enumerator.MoveNext())
{
IPAddress current = enumerator.Current;
text = text + current + " ,";
}
goto IL_88;
}
}
text = (text = "unknow ,");
}
IL_88:
if (text.Substring(text.Length - 1, 1) == ",")
{
text = text.Substring(0, text.Length - 1);
}
}
return text;
}
}
private void btnGetDnsinfo_Click(object sender, EventArgs e)
{
Dnsinfopay.Text = NetworkClass.GetDnsinfo();
}
}
}
文章评论 本文章有个评论