Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to create a small profile application for windows 7 to allow user to select a profile, which will then update the chosen NIC in the profile and change its IP, Subnet, Gateway, etc..

Found lots of examples on System.Management, but they don't seem to work in Win 7 and I was hoping to find a clear example using System.Net.

I apologize if this is a frequent question and I missed a post that would help. Thanks for any pointers
Posted

1 solution

Enumerate Nic First:
C#
/// <summary>/// Returns the list of Network Interfaces installed
/// </summary>
/// <returns>Array list of string</returns>
public static ArrayList GetNICNames()
{
	ArrayList nicNames = new ArrayList();

	ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
	ManagementObjectCollection moc = mc.GetInstances();

	foreach(ManagementObject mo in moc)
	{
		if((bool)mo["ipEnabled"])
		{
			nicNames.Add( mo["Caption"] );
		}
	}

	return nicNames;
}


Set DNS:
C#
public static void SetDNS(string nicName, string DnsSearchOrder)
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                // Make sure this is a IP enabled device. Not something like memory card or VM Ware
                if ((bool)mo["IPEnabled"])
                {
                    if (mo["Caption"].Equals(nicName))
                    {

                        ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");

                        newDNS["DNSServerSearchOrder"] = DnsSearchOrder.Split(',');

                        ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

                        break;
                    }
                }
            }


Set IP

C#
/// <summary>
/// Set IP for the specified network card name
/// </summary>
/// <param name="nicName">Caption of the network card</param>
/// <param name="IpAddresses">Comma delimited string containing one or more IP</param>
/// <param name="SubnetMask">Subnet mask</param>
/// <param name="Gateway">Gateway IP</param>
/// <param name="DnsSearchOrder">Comma delimited DNS IP</param>
public static void SetIP( string nicName, string IpAddresses, string SubnetMask, string Gateway, string DnsSearchOrder)
{
	ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
	ManagementObjectCollection moc = mc.GetInstances();

	foreach(ManagementObject mo in moc)
	{
		// Make sure this is a IP enabled device. Not something like memory card or VM Ware
		if( (bool)mo["IPEnabled"] )
		{
			if( mo["Caption"].Equals( nicName ) )
			{

				ManagementBaseObject newIP = mo.GetMethodParameters( "EnableStatic" );
				ManagementBaseObject newGate = mo.GetMethodParameters( "SetGateways" );
				ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );
						
				newGate[ "DefaultIPGateway" ] = new string[] { Gateway };
				newGate[ "GatewayCostMetric" ] = new int[] { 1 };

				newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
				newIP[ "SubnetMask" ] = new string[] { SubnetMask };

				newDNS[ "DNSServerSearchOrder" ] = DnsSearchOrder.Split(',');

				ManagementBaseObject setIP = mo.InvokeMethod( "EnableStatic", newIP, null);
				ManagementBaseObject setGateways = mo.InvokeMethod( "SetGateways", newGate, null);
				ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null);

				break;
			}
		}
	}
}
 
Share this answer
 
Comments
MoMoore 3-Sep-12 23:26pm    
Thanks for the reply, I did this very solution prior to posting my question, but I purposely disabled the "ipenabled" criteria trying to expose other NIC's, and it appears it failed because I chose NIC's that did not meet that requirement. Thanks for the response, greatly appreciated. I will work with IPENABLED devices moving forward.

Thanks
Kuthuparakkal 3-Sep-12 23:27pm    
you're most welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900