如何检测Windows网卡IPv4是否设置为自动获取IP地址
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
DHCPEnabledreturnstrueif the adapter is set to auto-obtain an IP via DHCP, andfalseif it's using a static IP configuration.- The added null checks prevent
IndexOutOfRangeExceptionscenarios where the adapter might not have active IP settings (e.g., disconnected Wi-Fi). - The
UpdateDhcpStatushelper 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

