Loading
0

C#获取本机网卡上的DNS地址方法

C#获取本机网卡上的DNS地址方法
功能:获取本机电脑第一块启用网卡上的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();

          
        }

    }
}

 


 

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:http://blog.tag.gg/showinfo-23-35573-0.html
上一篇:C#软件防止反编译之ConfuserEx.exe工具使用方法
下一篇:一段简洁有效的C#连接MSSQL数据库代码