C# 获取和修改 计算机名、IP、DNS等

本文介绍了使用C#获取计算机名、IP地址、DNS等网络配置信息的三种方法,包括System.Net、System.Management和System.Net.NetworkInformation命名空间的用法。同时,还提供了一个示例方法SetNetworkAdapter,用于修改这些网络配置。

 

首先,我们需要引入 System.Net 这个命名空间

using System.Net;

 

 

string pcName = Dns.GetHostName();  //取计算机名
string ip = Dns.GetHostByName(pcName).AddressList[0].ToString();    //取IP方法一(VS里提示已过时,汗!)
string ip2 = Dns.GetHostEntry(pcName).AddressList[0].ToString();    //取IP方法二

 

方法2,就是引入 System.Management  首先先添加程序集引用,然后用以下代码获取:

using System.Management;

 

ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach (ManagementObject nic in nics)
{
        if (Convert.ToBoolean(nic["ipEnabled"]) == true)
        {
            string mac = nic["MacAddress"].ToString();//Mac地址
              string ip = (nic["IPAddress"] as String[])[0];//IP地址
              string ipsubnet = (nic["IPSubnet"] as String[])[0];//子网掩码
              string ipgateway = (nic["DefaultIPGateway"] as String[])[0];//默认网关

              string dns = nic["DNSServerSearchOrder"] as String[])[0];  //DNS
         }
}

 

方法3,引入 System.Net.NetworkInformation 这个命名空间

 

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (adapter.NetworkInterfaceType.ToString().Equals("Ethernet"))
                {
                    IPInterfaceProperties ip = adapter.GetIPProperties();     //IP配置信息
                    if (ip.UnicastAddresses.Count > 0)
                    {
                        ip.UnicastAddresses[0].Address.ToString();   //IP地址
                        ip.UnicastAddresses[0].IPv4Mask.ToString();  //子网掩码
                    }
                    if (ip.GatewayAddresses.Count > 0)
                        ip.GatewayAddresses[0].Address.ToString();   //默认网关
                    if (ip.DnsAddresses.Count > 0)
                    {
                        ip.DnsAddresses[0].ToString();       //首选DNS服务器地址
                        if (ip.DnsAddresses.Count > 1)
                            ip.DnsAddresses[1].ToString();
                        ip.DnsAddresses[1].ToString();//备用DNS服务器地址
                    }
                }

 

接下来我们就来修改 计算机名、IP、DNS 等

 

static void SetNetworkAdapter()
        {
            ManagementBaseObject inPar
= null;
            ManagementBaseObject outPar
= null;
            ManagementClass mc
= new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc
= mc.GetInstances();
           
foreach( ManagementObject mo in moc )
            {
               
if( ! (bool) mo[ "IPEnabled" ] )
                   
continue;

               
//设置ip地址和子网掩码
                inPar = mo.GetMethodParameters( "EnableStatic" );
                inPar[
"IPAddress"] = new string[] { "10.22.21.111","192.168.10.9" };
                inPar[
"SubnetMask"] = new string[] { "255.255.255.0","255.255.255.0" };
                outPar
= mo.InvokeMethod( "EnableStatic", inPar, null );

               
//设置网关地址
                inPar = mo.GetMethodParameters("SetGateways");
                inPar[
"DefaultIPGateway"] = new string[] { "10.22.21.1","192.168.10.1"};
                outPar
= mo.InvokeMethod( "SetGateways", inPar, null );

               
//设置DNS
                inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                inPar[
"DNSServerSearchOrder"] = new string[] {"179.32.42.4","179.32.42.5"};
                outPar
= mo.InvokeMethod( "SetDNSServerSearchOrder" ,inPar,null);
                   
break;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值