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();

          
        }

    }
}

 


 
技术小学生微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://blog.tag.gg/showinfo-23-35573-0.html
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
上一篇:C#软件防止反编译之ConfuserEx.exe工具使用方法
下一篇:一段简洁有效的C#连接MSSQL数据库代码