Dev Language/C#

Mac Address Changer(맥 어드레스 변경)

타카스 류지 2015. 12. 30. 15:22

 

 

간단한 통합 매소드

 

 

nicid : 레지스트리의 폴더 주소(즉 index)

newmac : Mac 어드레스 주소(예 : 00 B0 08 A0 C1 1B)

 

newmac 값이 있을땐 설정 null 일땐 초기화

 

 

using System.Management;

 

 public static bool SetMAC(string nicid, string newmac)
        {
            bool ret = false;
            string baseReg = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\";

            using (RegistryKey bkey = GetBaseKey())
            using (RegistryKey key = bkey.OpenSubKey(baseReg + nicid))
            {
                if (key != null)
                {
                   
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\"+ nicid, RegistryKeyPermissionCheck.ReadWriteSubTree);
                   
                    if (rk != null)
                    {
                        if (newmac != null)
                        {
                            rk.SetValue("NetworkAddress", newmac);
                        }
                        else
                        {
                            rk.DeleteValue("NetworkAddress");
                        }
                       
                        rk.Close();
                    }


                    ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + nicid));

                    foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
                    {
                        o.InvokeMethod("Disable", null);
                        o.InvokeMethod("Enable", null);
                        ret = true;
                    }
                }
            }

            return ret;
        }