/// <summary> /// Duyệt qua các phần tử của AllMacDoc từ vị trí của lần duyệt trước đó /// </summary> /// <returns>Một MACEntity chứa thông tin của địa chỉ MAC</returns> public MACEntity IterateAllRow() { MACEntity resultMAC = new MACEntity(); MACHandler macHandler = new MACHandler(); XmlNodeList nodes = allMacDoc.DocumentElement.ChildNodes; for (int i = lastRowVisited + 1; i <= nodes.Count; i++) { string manufacturer = (nodes[i].Attributes["manufacturer"]?.Value) ?? ""; string mac = nodes[i].Attributes["mac"].Value; string hostName = nodes[i].Attributes["name"].Value; if (manufacturer == vmwareManufacturer || manufacturer == routerManufacturer) { continue; } try { if (!macHandler.IsOnline(mac)) //MAc này không online nên có thể dùng được { resultMAC.MAC = mac; resultMAC.Manufacturer = manufacturer; resultMAC.HostName = hostName; lastRowVisited = i; break; } } catch (ArgumentNullException) //MAC này không xuất hiện trong ARP table nên có thể dùng được { resultMAC.MAC = mac; resultMAC.Manufacturer = manufacturer; resultMAC.HostName = hostName; lastRowVisited = i; break; } } return(resultMAC); }
private void btnChangeMac_Click(object sender, EventArgs e) { if (cbFromAllMac.Checked) { MACEntity newMac = xmlHandler.IterateAllRow(); txtMac.Text = newMac.MAC; txtManufacturer.Text = newMac.Manufacturer; txtHostName.Text = newMac.HostName; } else { if (string.IsNullOrEmpty(txtMac.Text)) { MessageBox.Show("MAC value can't be empty", "Empty MAC"); return; } } if (SetNewMac(txtMac.Text)) { MessageBox.Show("Changed successfully", "Changed Registry Key", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Failed to change registry key value", "Changed Registry Key Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } try { MACHandler.DisableAndEnableNetworkAdapter("Ethernet"); } catch { MessageBox.Show("Failed to Disable and Enable Ethernet Adapter", "Disable And Enable Net Adapter", MessageBoxButtons.OK, MessageBoxIcon.Error); } }