You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下无法切换网卡为DHCP?C#代码返回错误码84求助

解决Windows网络适配器静态IP切换DHCP失败(错误码84)的问题

问题背景

需要实现Windows网络适配器在DHCP与静态IP间双向切换:从DHCP切换到静态IP正常,但从静态IP切换为DHCP时失败,调用EnableDHCP返回错误码84,测试环境为Windows 22H2 19045.5608、.NET 9,已以管理员权限运行代码。

原代码如下:

public static void WriteNetworkInWindows(WindowsNetworkInterface _windowsNetworkInterface, bool isDHCP)
{    
    if (_windowsNetworkInterface is { })
    {
        var _ipProperities = _windowsNetworkInterface.GetIPProperties();
        if (_ipProperities is { })
        {
            var _ipV4Properities = _ipProperities.GetIPv4Properties();
            if (_ipV4Properities is { })
            {
                var _index = _ipV4Properities.Index;
                ManagementObjectSearcher _searcher = new ManagementObjectSearcher(
                    $"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE InterfaceIndex = {_index}");                                      
                if(isDHCP)
                {                                    
                    foreach (ManagementObject i in _searcher.Get())
                    {                                 
                        var enableDhcpResult = (uint)i.InvokeMethod("EnableDHCP", null);                                            
                    }
                }
                else
                {
                    foreach (ManagementObject i in _searcher.Get())
                    {
                        ManagementBaseObject newIP = i.GetMethodParameters("EnableStatic");
                        newIP["IPAddress"] = new string[] { windowsNetworkAdapter.IPAddress };
                        newIP["SubnetMask"] = new string[] { windowsNetworkAdapter.SubSubMask };
                        i.InvokeMethod("EnableStatic", newIP, null);
                    }
                }
            }
        }
    }
}

问题分析

  1. 错误码84含义:对应WMI的ERROR_INVALID_PARAMETER(无效参数),本质原因是启用DHCP前未清理适配器现有的静态IP、网关及DNS配置,导致配置冲突。
  2. 变量名错误:静态IP配置部分使用了未定义的windowsNetworkAdapter变量,正确应为方法参数_windowsNetworkInterface。

解决方案

修改代码,在调用EnableDHCP前先清除现有静态配置(网关、DNS),同时修正变量名错误:

public static void WriteNetworkInWindows(WindowsNetworkInterface _windowsNetworkInterface, bool isDHCP)
{    
    if (_windowsNetworkInterface is null) return;
    
    var _ipProperities = _windowsNetworkInterface.GetIPProperties();
    if (_ipProperities is null) return;
    
    var _ipV4Properities = _ipProperities.GetIPv4Properties();
    if (_ipV4Properities is null) return;
    
    var _index = _ipV4Properities.Index;
    ManagementObjectSearcher _searcher = new ManagementObjectSearcher(
        $"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE InterfaceIndex = {_index}");                                      

    foreach (ManagementObject adapter in _searcher.Get())
    {
        if(isDHCP)
        {                                    
            // 先清除静态网关配置
            adapter.InvokeMethod("SetGateways", null, null);
            // 清除DNS服务器配置
            adapter.InvokeMethod("SetDNSServerSearchOrder", null);
            // 启用DHCP
            var enableDhcpResult = (uint)adapter.InvokeMethod("EnableDHCP", null);
        }
        else
        {
            ManagementBaseObject newIP = adapter.GetMethodParameters("EnableStatic");
            // 修正变量名,使用方法传入的_windowsNetworkInterface
            newIP["IPAddress"] = new string[] { _windowsNetworkInterface.IPAddress };
            newIP["SubnetMask"] = new string[] { _windowsNetworkInterface.SubSubMask };
            adapter.InvokeMethod("EnableStatic", newIP, null);
            
            // 若需要设置网关,可添加以下代码
            // ManagementBaseObject newGateway = adapter.GetMethodParameters("SetGateways");
            // newGateway["DefaultIPGateway"] = new string[] { _windowsNetworkInterface.Gateway };
            // adapter.InvokeMethod("SetGateways", newGateway, null);
            
            // 若需要设置DNS,可添加以下代码
            // adapter.InvokeMethod("SetDNSServerSearchOrder", new string[] { _windowsNetworkInterface.DnsServer });
        }
    }
}

关键修改点

  • 启用DHCP前调用SetGateways和SetDNSServerSearchOrder清除现有静态配置,避免冲突
  • 修正静态IP配置部分的变量名错误,确保使用正确的方法参数
  • 简化空值判断逻辑,提升代码可读性

内容的提问来源于stack exchange,提问作者Volfogg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 15:43:12