Ejemplo n.º 1
0
        private void btnThisMacOnline_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMacToCheck.Text))
            {
                MessageBox.Show("MAC to check can't be empty", "Empty MAC");
                return;
            }

            MACHandler macHandler = new MACHandler();

            try
            {
                if (macHandler.IsOnline(MACHandler.ConvertToOnlyNumberMac(txtMacToCheck.Text)))
                {
                    MessageBox.Show("The PC of this MAC is online now", "Online", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("The PC of this MAC is offline now", "Offline", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show($"{ex.Message}\n" + $"Error: {ex.InnerException.Message}", "This MAC Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error when ping: {ex.Message}", "Ping Error");
            }
        }
Ejemplo n.º 2
0
        private bool SetNewMac(string newMac)
        {
            string newMacWithoutSeparator = MACHandler.ConvertToOnlyNumberMac(newMac);

            RegistryKey rKey = null;

            try
            {
                rKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0002", true);
                rKey.SetValue("NetworkAddress", newMacWithoutSeparator);

                rKey.Close();

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
            finally
            {
                rKey.Close();
            }
        }
Ejemplo n.º 3
0
        private void LoadCurrentMac()
        {
            RegistryKey rKey;
            string      macValue;

            rKey     = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0002", true);
            macValue = rKey.GetValue("NetworkAddress").ToString();

            rKey.Close();
            lbCurrentMac.Text = MACHandler.ConvertToSeparatedMac(macValue, ":");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Kiểm tra xem địa chỉ MAC có online không
        /// </summary>
        /// <param name="macWithoutSeparator">MAC không có dấu phân cách</param>
        /// <returns></returns>
        public bool IsOnline(string macWithoutSeparator)
        {
            string macForARPTable = MACHandler.ConvertToSeparatedMac(macWithoutSeparator, "-");
            string ipOfMAC        = finder.FindIpAddressByMacAddress(macForARPTable);

            try
            {
                return(DeviceScanner.IsHostAccessible(ipOfMAC));
            }
            catch (ArgumentNullException e)
            {
                throw new ArgumentNullException("This MAC is not in ARP Table", e); //có thể dùng được MAC này
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        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);
            }
        }