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

如何检测Windows网卡IPv4是否设置为自动获取IP地址

Detecting DHCP vs Static IP Configuration in C# WinForm

Hey there! I get that you've built a solid WinForm app to manage IPv4 settings, but the missing piece is detecting whether the target network adapter is set to automatically obtain an IP address (DHCP) or using a static configuration when your app launches. Let's solve this together.

The Key Property You're Missing: DHCPEnabled

The Win32_NetworkAdapterConfiguration WMI class actually has a built-in boolean property called DHCPEnabled that directly tells you if DHCP is enabled for the adapter. This is exactly what corresponds to the "自动获取IP地址" option in Windows settings.

Updating Your Getip() Method

You can modify your existing Getip() method to check this property, and also add some null safety to avoid potential crashes (since properties like IPAddress or DefaultIPGateway might be null if the adapter isn't connected). Here's the revised version:

public bool Getip()
{
    bool isDhcpEnabled = false;
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();

    foreach (ManagementObject mo in moc)
    {
        if (!chipset_selector.Items.Contains(mo["Caption"]))
            chipset_selector.Items.Add(mo["Caption"]);

        if (mo["Caption"].Equals(_wifi))
        {
            // Check if DHCP is enabled for the adapter
            if (mo["DHCPEnabled"] != null)
                isDhcpEnabled = (bool)mo["DHCPEnabled"];

            // Safely retrieve IP settings with null checks to avoid crashes
            ipadd = mo["IPAddress"] is string[] ipAddresses && ipAddresses.Length > 0 ? ipAddresses[0] : "N/A";
            subnet = mo["IPSubnet"] is string[] subnets && subnets.Length > 0 ? subnets[0] : "N/A";
            gateway = mo["DefaultIPGateway"] is string[] gateways && gateways.Length > 0 ? gateways[0] : "N/A";
            dns = mo["DNSServerSearchOrder"] is string[] dnsServers && dnsServers.Length > 0 ? dnsServers[0] : "N/A";
            
            break;
        }
    }

    // Pass the DHCP status to update your UI
    UpdateDhcpStatus(isDhcpEnabled);
    // Optional: return the status if you need to use it elsewhere in your app
    return isDhcpEnabled; 
}

// Helper method to display DHCP status in your UI
private void UpdateDhcpStatus(bool isDhcpEnabled)
{
    if (isDhcpEnabled)
        lbl_ip_config_status.Text = "IP Configuration: Automatic (DHCP)";
    else
        lbl_ip_config_status.Text = "IP Configuration: Static";
}

How This Works

  • DHCPEnabled returns true if the adapter is set to auto-obtain an IP via DHCP, and false if it's using a static IP configuration.
  • The added null checks prevent IndexOutOfRangeException scenarios where the adapter might not have active IP settings (e.g., disconnected Wi-Fi).
  • The UpdateDhcpStatus helper method makes it straightforward to reflect the current configuration state in your app's interface.

Calling This on App Startup

Just invoke Getip() in your form's load event (like Form_Load), and it will detect the DHCP status immediately when your app launches, updating your UI automatically.

Quick Note About Permissions

Depending on your Windows version, you might need to run the app with administrator privileges to access WMI network configuration properties reliably. You can add an application manifest to your project to enforce this requirement if needed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:28:16