I want to change the network card configuration as the IP address, subnet, gateway and DNSs. All this using ASP.NET 4.0 with C# deployed in Windows XP IIS 5.1. I'm using WMI for this purpose, but when I run the code not work.
Specifically I can show the IP, Subnet, Gateway and DNS of network. But I can't set the values.
My idea is configure the network card remotely (as a router).
What I have tried:
I tried to make some things similar to this example:
SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places[
^]
The code for the WMI helper class:
<pre>using System;
using System.Collections;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace NetworkConfig
{
public class WMI_helper
{
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;
}
public static void GetIP(string nicName, out string[] ipAddresses, out string[] subnets, out string[] gateways, out string[] dnses)
{
ipAddresses = null;
subnets = null;
gateways = null;
dnses = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["ipEnabled"])
{
if (mo["Caption"].Equals(nicName))
{
ipAddresses = (string[]) mo["IPAddress"];
subnets = (string[]) mo["IPSubnet"];
gateways = (string[])mo["DefaultIPGateway"];
dnses = (string[]) mo["DNSServerSearchOrder"];
break;
}
}
}
}
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)
{
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;
}
}
}
}
public static void SetDHCP(string nicName)
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["Caption"].Equals(nicName))
{
ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = null;
ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);
ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
}
}
}
}
}