/// <summary> /// 可行 ,但官网已经不推荐使用 /// </summary> /// <param name="ni"></param> /// <returns></returns> private static bool IsPhysicalAdapter(NetworkInterface ni) { bool isPhysical = false; ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", string.Format(@"SELECT * FROM Win32_NetworkAdapter WHERE GUID='{0}' AND NOT PNPDeviceID LIKE 'ROOT\\%'", ni.Id)); foreach (ManagementObject share in searcher.Get()) { isPhysical = Convert.ToBoolean(share.Properties["PhysicalAdapter"].Value); break; } return isPhysical; } /// <summary> /// 可行,官网最新方式 /// </summary> public static void IsPhysicalAdapter2() { string query = "SELECT * From MSFT_NetAdapter"; ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\StandardCimv2",query); ManagementObjectCollection adaptersCollection = searcher.Get(); foreach (ManagementObject adapterWmiObject in adaptersCollection) //System.Management.ManagementException { Trace.WriteLine(adapterWmiObject["ConnectorPresent"] +"------"+adapterWmiObject["Virtual"]);//判断Virtual字段,可以判定 Trace.WriteLine("PNPDeviceID:"+adapterWmiObject["PNPDeviceID"] ); Trace.WriteLine("Caption:"+adapterWmiObject["Caption"]); Trace.WriteLine("Description:"+adapterWmiObject["Description"]); Trace.WriteLine("Name:"+adapterWmiObject["Name"]); Trace.WriteLine(adapterWmiObject["DeviceID"]); Trace.WriteLine("InterfaceType:"+adapterWmiObject["InterfaceType"]); //Trace.WriteLine(adapterWmiObject["Caption"]); //Trace.WriteLine(adapterWmiObject["Caption"]); } }
public static void GetAll() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (var item in nics) { Trace.WriteLine(IsPhysicalAdapter( item)); Trace.WriteLine("Id:" + item.Id); Trace.WriteLine("NetworkInterfaceType:" + item.NetworkInterfaceType); Trace.WriteLine("Description:" + item.Description); Trace.WriteLine("Name:" + item.Name); Trace.WriteLine("PhysicalAddress:" + item.GetPhysicalAddress().ToString()); IPInterfaceProperties ip = item.GetIPProperties(); Trace.WriteLine("count:" + ip.UnicastAddresses.Count); Trace.WriteLine("Address:" + ip.UnicastAddresses[1].Address); Trace.WriteLine("IPv4Mask:" + ip.UnicastAddresses[1].IPv4Mask); Trace.WriteLine("dns count:" + ip.DnsAddresses.Count); Trace.WriteLine("Dns Address:" + ip.DnsAddresses[0]); Trace.WriteLine("gateway count:" + ip.GatewayAddresses.Count); Trace.WriteLine("gateway Address:" + (ip.GatewayAddresses.Count > 0 ? ip.GatewayAddresses[0].Address.ToString() : "null")); } } public static void UpdateInfo(string sMac) { ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration"); string mac = ""; foreach (ManagementObject mo in mos.Get()) { if (!(bool)mo["IPEnabled"]) { continue; } Trace.WriteLine(mo["MacAddress"]); mac = mo["MacAddress"].ToString(); mac = mac.Replace(":",""); mac = mac.Replace("-", ""); if (sMac.Equals(mac,StringComparison.OrdinalIgnoreCase)) { ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"); ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways"); ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); newIP["IPAddress"] = new string[] { "192.168.56.1" }; newIP["SubnetMask"] = new string[] { "255.255.255.0" }; newGateway["DefaultIPGateway"] = new string[] { "" }; newDNS["DNSServerSearchOrder"] = new string[] { "0.0.0.0" }; ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null); ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null); // ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS,null); //DNS的地址清空 mo.InvokeMethod("SetDNSServerSearchOrder", null); //mo.InvokeMethod("EnableDHCP", null); //开启DHCP } } }
标签:读取,Trace,C#,ip,mo,网卡,WriteLine,adapterWmiObject From: https://www.cnblogs.com/xbj-hyml/p/17245183.html