我正在尝试以编程方式更改系统MAC ID,并且正在使用以下代码,但是由于RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid)
为空,因此它不起作用。
我该如何解决这个问题。如果这很重要,我正在运行Win7 64位操作系统。
也public const string baseReg = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\";
nicid =第一个网络适配器的当前mac ID
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(Constants.baseReg + nicid))
{
if (key != null)
{
key.SetValue("NetworkAddress", newmac, RegistryValueKind.String);
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;
}
}
}
答案 0 :(得分:1)
因此,经过数小时的挖掘和浏览,我解决了此问题。 在侧面说明,这有一个问题,它仅使用ActiveAdapterMAC列表中的第一个适配器。
PSS-此代码不是最好的代码,我需要做的修复很少。但是我相信这可以使您朝正确的方向前进。 这是获取当前活动网络适配器NICID的代码,然后使用该代码来设置MAC ID
public static void GetAllNetworkAdapters()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters.Where(a => a.OperationalStatus == OperationalStatus.Up))
{
ActiveAdapters.Add(adapter.Id);
string MAC = adapter.GetPhysicalAddress().ToString();
ActiveAdapterMAC.Add(MAC);
Logger.LogGenericText($"Adapter Properties => {adapter.Id} / {MAC}");
}
}
public static string GetSubKeyValue()
{
RegistryKey Base = GetBaseKey();
RegistryKey Key = Base.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}", true);
string SelectedKey = null;
string[] Values = Key.GetSubKeyNames();
try
{
foreach (var key in Values)
{
RegistryKey ValueChecker = Base.OpenSubKey(Constants.baseReg + key, true);
string NetCFID = ValueChecker.GetValue("NetCfgInstanceId").ToString();
if (NetCFID == ActiveAdapters[0])
{
SelectedKey = key;
}
ValueChecker.Close();
}
}
catch(Exception ex)
{
if(ex is SecurityException)
{
Key.Close();
return SelectedKey;
}
}
Key.Close();
return SelectedKey;
}
public static void ConfigMAC(bool Reset = false, bool RegisterDefault = false)
{
RegistryKey Base = GetBaseKey();
string NICID = GetSubKeyValue();
string NewMAC = GenerateMACAddress();
CachedNICID = NICID;
if (Config.WifiConnection)
{
NewMAC = GetRandomWifiMacAddress();
}
if (RegisterDefault)
{
string CurrentMAC = GetMacIDWithCache();
Constants.CurrentMACID = CurrentMAC;
try
{
RegistryKey RegisterKey = Base.OpenSubKey(Constants.baseReg + NICID, true);
if (RegisterKey.GetValue(Constants.DefaultMAC) != null)
{
RegisterKey.Close();
return;
}
else
{
RegisterKey.SetValue(Constants.DefaultMAC, CurrentMAC);
RegisterKey.Close();
return;
}
}
catch (Exception ex)
{
Logger.LogGenericText(ex.ToString());
return;
}
}
if (Reset)
{
DeleteKey(NICID, true);
return;
}
Logger.LogGenericText("Opening Sub Key => " + Constants.baseReg + NICID);
RegistryKey Key = Base.OpenSubKey(Constants.baseReg + NICID, true);
string[] KeyValues = Key.GetValueNames();
if (!KeyValues.Contains(Constants.MacValue))
{
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
else
{
DeleteKey(NICID, false);
Key.SetValue(Constants.MacValue, NewMAC);
Logger.LogGenericText("Value Set for " + Constants.MacValue + " as " + NewMAC);
Constants.CurrentMACID = NewMAC;
Task.Run(() => ResetAdapter(NICID));
}
Key.Close();
}
public static void DeleteKey(string Nicid, bool Restart)
{
Logger.LogGenericText("Deleting Previous Changed MAC...");
RegistryKey Base = GetBaseKey();
RegistryKey ResetKey = Base.OpenSubKey(Constants.baseReg + Nicid, true);
ResetKey.DeleteValue(Constants.MacValue);
ResetKey.Close();
if (Restart)
{
Task.Run(() => ResetAdapter(Nicid));
return;
}
else
{
return;
}
}
public static void ResetAdapter(string Nicid)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher
(new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + Nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
Logger.LogGenericText("Disabling Adapter and Waiting for 6 Seconds.");
o.InvokeMethod("Disable", null);
Task.Delay(6000).Wait();
o.InvokeMethod("Enable", null);
Logger.LogGenericText("New MAC ID Set Sucessfully!");
}
}
它如何工作? 我使用了这种逻辑
该软件仅将值写入Windows注册表。启用网络适配器设备后,Windows会在注册表项HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Class {4D36E972-E325-11CE-BFC1- 08002bE10318}中搜索注册表值“ NetworkAddress”。 0001]。如果存在一个值,则Windows将使用它作为MAC地址,否则,Windows将使用制造商提供的硬编码MAC地址。某些网络适配器驱动程序具有内置的此功能。可以在Windows设备管理器中网络适配器的设备属性的“高级设置”选项卡中找到它。
来自=> TMAC网站。