none
C#如何设置指定网卡的IP地址和网关相关信息? RRS feed

  • 问题

  • C#如何设置指定网卡的IP地址和网关相关信息,我是用WMI的方法设置,发现所有的网卡都设置了相同的网关和DNS。
    • 已编辑 宜久 2015年5月17日 2:30
    2015年5月17日 2:29

全部回复

  • 你好:

    尝试使用WMI来设置网卡信息,参考以下代码(来自网络):

    using System;
    using System.Management;
    
    namespace WindowsFormsApplication_CS
    {
        class NetworkManagement
        {
            /// <summary>
            /// Set's a new IP Address and it's Submask of the local machine
            /// </summary>
            /// <param name="ip_address">The IP Address</param>
            /// <param name="subnet_mask">The Submask IP Address</param>
            /// <remarks>Requires a reference to the System.Management namespace</remarks>
            public void setIP(string ip_address, string subnet_mask)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMC.GetInstances();
    
                foreach (ManagementObject objMO in objMOC)
                {
                    if ((bool)objMO["IPEnabled"])
                    {
                        try
                        {
                            ManagementBaseObject setIP;
                            ManagementBaseObject newIP =
                                objMO.GetMethodParameters("EnableStatic");
    
                            newIP["IPAddress"] = new string[] { ip_address };
                            newIP["SubnetMask"] = new string[] { subnet_mask };
    
                            setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
    
    
                    }
                }
            }
            /// <summary>
            /// Set's a new Gateway address of the local machine
            /// </summary>
            /// <param name="gateway">The Gateway IP Address</param>
            /// <remarks>Requires a reference to the System.Management namespace</remarks>
            public void setGateway(string gateway)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMC.GetInstances();
    
                foreach (ManagementObject objMO in objMOC)
                {
                    if ((bool)objMO["IPEnabled"])
                    {
                        try
                        {
                            ManagementBaseObject setGateway;
                            ManagementBaseObject newGateway =
                                objMO.GetMethodParameters("SetGateways");
    
                            newGateway["DefaultIPGateway"] = new string[] { gateway };
                            newGateway["GatewayCostMetric"] = new int[] { 1 };
    
                            setGateway = objMO.InvokeMethod("SetGateways", newGateway, null);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                }
            }
            /// <summary>
            /// Set's the DNS Server of the local machine
            /// </summary>
            /// <param name="NIC">NIC address</param>
            /// <param name="DNS">DNS server address</param>
            /// <remarks>Requires a reference to the System.Management namespace</remarks>
            public void setDNS(string NIC, string DNS)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMC.GetInstances();
    
                foreach (ManagementObject objMO in objMOC)
                {
                    if ((bool)objMO["IPEnabled"])
                    {
                        // if you are using the System.Net.NetworkInformation.NetworkInterface you'll need to change this line to if (objMO["Caption"].ToString().Contains(NIC)) and pass in the Description property instead of the name 
                        if (objMO["Caption"].Equals(NIC))
                        {
                            try
                            {
                                ManagementBaseObject newDNS =
                                    objMO.GetMethodParameters("SetDNSServerSearchOrder");
                                newDNS["DNSServerSearchOrder"] = DNS.Split(',');
                                ManagementBaseObject setDNS =
                                    objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }
                    }
                }
            }
            /// <summary>
            /// Set's WINS of the local machine
            /// </summary>
            /// <param name="NIC">NIC Address</param>
            /// <param name="priWINS">Primary WINS server address</param>
            /// <param name="secWINS">Secondary WINS server address</param>
            /// <remarks>Requires a reference to the System.Management namespace</remarks>
            public void setWINS(string NIC, string priWINS, string secWINS)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMC.GetInstances();
    
                foreach (ManagementObject objMO in objMOC)
                {
                    if ((bool)objMO["IPEnabled"])
                    {
                        if (objMO["Caption"].Equals(NIC))
                        {
                            try
                            {
                                ManagementBaseObject setWINS;
                                ManagementBaseObject wins =
                                objMO.GetMethodParameters("SetWINSServer");
                                wins.SetPropertyValue("WINSPrimaryServer", priWINS);
                                wins.SetPropertyValue("WINSSecondaryServer", secWINS);
    
                                setWINS = objMO.InvokeMethod("SetWINSServer", wins, null);
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }
                    }
                }
            } 
        }
    }

    更多WMI属性请参考文档:

    Win32_NetworkAdapterConfiguration class


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年5月18日 7:53
    版主
  • 这段代码能否指定网卡?因为我的程序运行的计算机都带都块网卡,有的还有虚拟网卡,所以网上的一些代码都会把所有的网卡设置成同一个网关和DNS
    2015年5月19日 2:59
  • 你可以模仿后面的两个方法,通过Caption属性来判断NIC,也就是网卡地址。

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年5月19日 9:06
    版主