I'm writing a tool that allows our System Administrators to setup PCs before they're deployed. Among other things, it's supposed to allow them to set the IP address, Subnet mask and Default Gateway for a PC before it's connected to the company LAN.
It works under Windows XP, but fails miserably under Windows 7. Under Win7, it can't even find the current IP address (and settings). I knowwhy, but I need to know if there is a way around it.
Here is the code I use to detect the 3 settings. I found it here on the MSDN forums:
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces()) { if (f.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipInterface = f.GetIPProperties(); if( ipInterface.GetIPv4Properties() == null ) { // we aren't interested in loopback interfaces continue; } foreach (UnicastIPAddressInformation ua in ipInterface.UnicastAddresses) { if (ua.IPv4Mask != null) {
// save the IP address, subnet mask and default gateway ws.IpAddress = ua.Address; ws.SubnetIP = ua.IPv4Mask; ws.DefaultGateway = f.GetIPProperties().GatewayAddresses.FirstOrDefault().Address; // Get the MAC address of this NIC PhysicalAddress testMAC = f.GetPhysicalAddress(); ws.MacAddress = testMAC.ToString(); } } } }
From what I've been able to learn--from here and other places--it fails because if an Eternet adapter isn't plugged into the Eternet port, Win7 claims the NIC is disabled. I need to set the IP before it's connected to the LAN, so this won't do. Even if I skip the "loopback" section, the IPv4Mask section fails (that is, it claims IPv4 doesn't exist for the adapter).
Is there any way to detect and set the IP address (& related) settings without being connected to a LAN?