C#完成獲得設置IP地址小對象。本站提示廣大學習愛好者:(C#完成獲得設置IP地址小對象)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成獲得設置IP地址小對象正文
界面後果圖以下:
報表界面
說下症結代碼
須要開啟 Windows Management Instrumentation辦事(默許曾經開啟),在法式中須要增長 Management援用。
重要有NetworkAdapter(保留適配器的IP地址,網關,子網掩碼,設置IP方面等 ),NetworkAdapterUtil(重要是NetworkAdapter類的聚集 )兩個類。
在windows8 須要在app.manifest文件中
修正設置裝備擺設 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,否則就算有治理員權限也修正不了IP地址。
1 設置IP地址的代碼。(設置IP地址,修正IP地址須要治理員權限)
/// <summary> /// 收集適配器類 /// </summary> public class NetworkAdapter { /// <summary> /// 設置IP地址 /// </summary> /// <param name="ip"></param> /// <param name="submask"></param> /// <param name="getway"></param> /// <param name="dns"></param> private bool SetIPAddress(string[] ip, string[] submask, string[] getway, string[] dns) { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; string str = ""; foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; if (this.NetworkInterfaceID == mo["SettingID"].ToString()) { if (ip != null && submask != null) { string caption = mo["Caption"].ToString(); //描寫 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = ip; inPar["SubnetMask"] = submask; outPar = mo.InvokeMethod("EnableStatic", inPar, null); str = outPar["returnvalue"].ToString(); return (str=="0"||str=="1")?true:false; //獲得操作設置IP的前往值, 可依據前往值去確認IP能否設置勝利。 0或1表現勝利 // 前往值解釋網址: https://msdn.microsoft.com/en-us/library/aa393301(v=vs.85).aspx } if(getway!=null) { inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = getway; outPar = mo.InvokeMethod("SetGateways", inPar, null); str = outPar["returnvalue"].ToString(); return (str == "0" || str == "1") ? true : false; } if (dns != null) { inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = dns; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); str = outPar["returnvalue"].ToString(); return (str == "0" || str == "1") ? true : false; } } } return false; } }
2 設置IP為主動獲得
/// <summary> /// 啟用DHCP辦事 /// </summary> public void EnableDHCP() { ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; if (mo["SettingID"].ToString() == this.NetworkInterfaceID) //網卡接口標識能否相等, 相當只設置指定適配器IP地址 { mo.InvokeMethod("SetDNSServerSearchOrder", null); mo.InvokeMethod("EnableDHCP", null); } } }
3. 獲得一切適配器
public class NetworkAdapterUtil { /// <summary> /// 獲得一切適配器類型,適配器被禁用則不克不及獲得到 /// </summary> /// <returns></returns> public List<NetworkAdapter> GetAllNetworkAdapters() //假如適配器被禁用則不克不及獲得到 { IEnumerable<NetworkInterface> adapters = NetworkInterface.GetAllNetworkInterfaces(); //獲得一切適配器 return GetNetworkAdapters(adapters); } /// <summary> /// 依據前提獲得IP地址聚集, /// </summary> /// <param name="adapters">收集接口地址聚集</param> /// <param name="adapterTypes">收集銜接狀況,如,UP,DOWN等</param> /// <returns></returns> private List<NetworkAdapter> GetNetworkAdapters(IEnumerable<NetworkInterface> adapters, params NetworkInterfaceType[] networkInterfaceTypes) { adapterList = new List<NetworkAdapter>(); foreach (NetworkInterface adapter in adapters) { if (networkInterfaceTypes.Length <=0) //假如沒傳可選參數,就查詢一切 { if (adapter != null) { NetworkAdapter adp = SetNetworkAdapterValue(adapter); adapterList.Add(adp); } else { return null; } } else //過濾查詢數據 { foreach (NetworkInterfaceType networkInterfaceType in networkInterfaceTypes) { if (adapter.NetworkInterfaceType.ToString().Equals(networkInterfaceType.ToString())) { adapterList.Add(SetNetworkAdapterValue(adapter)); break; //加入以後輪回 } } } } return adapterList; } /// <summary> /// 設置收集適配器信息 /// </summary> /// <param name="adapter"></param> /// <returns></returns> private NetworkAdapter SetNetworkAdapterValue(NetworkInterface adapter) { NetworkAdapter networkAdapter = new NetworkAdapter(); IPInterfaceProperties ips = adapter.GetIPProperties(); networkAdapter.Description = adapter.Name; networkAdapter.NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(); networkAdapter.Speed = adapter.Speed / 1000 / 1000 + "MB"; //速度 networkAdapter.MacAddress = adapter.GetPhysicalAddress(); //物理地址聚集 networkAdapter.NetworkInterfaceID = adapter.Id;//收集適配器標識符 networkAdapter.Getwaryes = ips.GatewayAddresses; //網關地址聚集 networkAdapter.IPAddresses = ips.UnicastAddresses; //IP地址聚集 networkAdapter.DhcpServerAddresses = ips.DhcpServerAddresses;//DHCP地址聚集 networkAdapter.IsDhcpEnabled =ips.GetIPv4Properties()==null?false: ips.GetIPv4Properties().IsDhcpEnabled; //能否啟用DHCP辦事 IPInterfaceProperties adapterProperties = adapter.GetIPProperties();//獲得IPInterfaceProperties實例 networkAdapter.DnsAddresses = adapterProperties.DnsAddresses; //獲得並顯示DNS辦事器IP地址信息 聚集 return networkAdapter; } } }
4 啟用, 禁用適配器代碼
/// <summary> /// 啟用一切適配器 /// </summary> /// <returns></returns> public void EnableAllAdapters() { // ManagementClass wmi = new ManagementClass("Win32_NetworkAdapter"); // ManagementObjectCollection moc = wmi.GetInstances(); System.Management.ManagementObjectSearcher moc = new System.Management.ManagementObjectSearcher("Select * from Win32_NetworkAdapter where NetEnabled!=null "); foreach (System.Management.ManagementObject mo in moc.Get()) { //if (!(bool)mo["NetEnabled"]) // continue; string capation = mo["Caption"].ToString(); string descrption =mo["Description"].ToString(); mo.InvokeMethod("Enable",null); } } /// <summary> /// 禁用一切適配器 /// </summary> public void DisableAllAdapters() { // ManagementClass wmi = new ManagementClass("Win32_NetworkAdapter"); // ManagementObjectCollection moc = wmi.GetInstances(); System.Management.ManagementObjectSearcher moc = new System.Management.ManagementObjectSearcher("Select * from Win32_NetworkAdapter where NetEnabled!=null "); foreach (System.Management.ManagementObject mo in moc.Get()) { //if ((bool)mo["NetEnabled"]) // continue; string capation = mo["Caption"].ToString(); string descrption = mo["Description"].ToString(); mo.InvokeMethod("Disable", null); } }
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。