1、經過WMI獲取物理適配器序號windows
NetEnabled: 是否啓用了適配器,True爲啓用,False爲禁用;
PhysicalAdapter: 適配器是否物理或邏輯適配器,True爲物理,False爲邏輯;網絡
public static List<int> GetUseIndex() { List<int> list = new List<int>(); ManagementClass mc = new ManagementClass("Win32_NetworkAdapter"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { bool p1 = Convert.ToBoolean(mo["NetEnabled"]); bool p2 = Convert.ToBoolean(mo["PhysicalAdapter"]); if (p1 && p2) { list.Add(Convert.ToInt32((mo["Index"]))); } } return list; }
2、過濾虛擬網卡、無線網卡spa
經過「Characteristics」這個值來肯定網卡的類型是虛擬網卡仍是物理網卡。Characteristics 值在註冊表在HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\【鏈接索引號】\下,在windows中,Characteristics 的取值以下,Characteristics項能夠有1個或多個以下的值(多值應計算總和):【備註:在Windows7和Windows10下確認,Characteristics爲dword,不可能多個值,這裏的多個值,計算總和暫時未知。】code
0x1blog |
NCF_VIRTUAL索引 |
說明組件是個虛擬適配器dns |
0x2接口 |
NCF_SOFTWARE_ENUMERATEDip |
說明組件是一個軟件模擬的適配器ci |
0x4 |
NCF_PHYSICAL |
說明組件是一個物理適配器 |
0x8 |
NCF_HIDDEN |
說明組件不顯示用戶接口 |
0x10 |
NCF_NO_SERVICE |
說明組件沒有相關的服務(設備驅動程序) |
0x20 |
NCF_NOT_USER_REMOVABLE |
說明不能被用戶刪除(例如,經過控制面板或設備管理器) |
0x40 |
NCF_MULTIPORT_INSTANCED_ADAPTER |
說明組件有多個端口,每一個端口做爲單獨的設備安裝。每一個端口有本身的hw_id(組件ID) 並可被單獨安裝,這隻適合於EISA適配器 |
0x80 |
NCF_HAS_UI |
說明組件支持用戶接口(例如,Advanced Page或Customer Properties Sheet) |
0x400 |
NCF_FILTER |
說明組件是一個過濾器 |
若是是物理網卡:Characteristics & NCF_PHYSICAL ==NCF_PHYSICAL
判斷有線網卡和無線網卡,註冊表路徑:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\【鏈接索引號】\Ndi\Interfaces
路徑下的鍵值:LowerRange,若是Value包含wifi或者wlan,(驗證的兩臺設備Value=「wlan,ethernet,vwifi」),表示無線網卡,具體代碼以下:
public static bool GetWiredIndex(ref int index, ref string msg) { try { List<int> allenable = GetUseIndex(); if (allenable.Count == 0) { msg = "未查找到有效網絡鏈接"; return false; } List<int> allReal = new List<int>(); foreach (int ii in allenable) { var vv = Convert.ToInt32(GetCharacteristics(@"SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000" + ii, "Characteristics")); if ((vv & 0x4) == 0x4)//區分物理網卡、虛擬網卡 { allReal.Add(ii); } } if (allReal.Count == 0) { msg = "未查找到有效物理網卡"; return false; } int rv = -1; foreach (int ii in allReal) { var vv = GetCharacteristics(@"SYSTEM\ControlSet001\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000" + ii + @"\Ndi\Interfaces", "LowerRange").ToString(); if (!vv.Contains("wifi") && !vv.Contains("wlan")) { rv = ii; break; } } if (rv == -1) { msg = "未查找到有效有線網卡"; return false; } else { index = rv; return true; } } catch (Exception ex) { msg = ex.Message; return false; } } private static object GetCharacteristics(string name, string key) { object registData; using (RegistryKey hkml = Registry.LocalMachine) { RegistryKey software = hkml.OpenSubKey(name, true); registData = software.GetValue(key).ToString(); } return registData; }
3、設置有線網卡IP、子網掩碼、網關、DNS
public static bool SetWiredIP(int index, string ip, string subnetMask, string ipGateway, string dns) {
ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (Convert.ToInt32(mo["Index"]) == index) { //設置ip地址和子網掩碼 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = new string[] { ip }; inPar["SubnetMask"] = new string[] { subnetMask }; outPar = mo.InvokeMethod("EnableStatic", inPar, null); //設置網關地址 inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = new string[] { ipGateway }; outPar = mo.InvokeMethod("SetGateways", inPar, null); //設置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = new string[] { dns }; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); return true; } } return false; }