This changes the IP of msloop using WMI(Win32_NetworkAdapterConfiguration)
, which takes about 1~2sec.
But, if I work on another computer, it works much much slower, for about 50~60sec.
My Computer:
OS: Window 7 Professional
RAM: 8GB
Another Computer:
OS: Window XP SP3
RAM: 2GB
public void setIP () {
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC) {
if (!(bool) objMO["IPEnabled"]) {
continue;
} else if ((String) objMO["ServiceName"] != "msloop") {
continue;
}
try {
ManagementBaseObject objNewIP = objMO.GetMethodParameters("EnableStatic");
ManagementBaseObject objNewGate = objMO.GetMethodParameters("SetGateways");
objNewIP["IPAddress"] = new string[] { "110.110.110.1" };
objNewIP["SubnetMask"] = new string[] { "255.255.255.252" };
objNewGate["GatewayCostMetric"] = new string[] { "1" };
objMO.InvokeMethod("EnableStatic", objNewIP, null);
Console.WriteLine("EnableStatic End");
objMO.InvokeMethod("SetGateways", objNewGate, null);
Console.WriteLine("SetGateways End");
Console.WriteLine("Change OK");
Console.WriteLine("==================================================================");
} catch (Exception ex) {
MessageBox.Show("Unable to Set IP : " + ex.Message);
}
}
}
The result is always "Change OK". It works on XP, but very slowly. Is the problem with the OP or the RAM?
2 Answers 2
Can't help with the speed question but some generic remarks:
Both
ManagementClass
andManagementObjectCollection
areIDisposable
and should therefore be wrapped in ausing
block to make sure any unmanaged resources are properly released.Prefixing all your local variables with
obj
smells like hungarian notation and is generally discouraged. It adds not value and just clutter. Your variable names should indicate what they represent and not what type they are.In C# land the common naming convention for methods is
PascalCase
and notcamelCase
. It pays to stick to general naming conventions for the ecosystem you are working in as it will make your code look more familiar to other developers.
This should be considered an extension to ChrisWue's answer which discusses IDisposable
objects and appropriate method naming.
objNewIP["IPAddress"] = new string[] { "110.110.110.1" };
Has a couple of problems, there is a 'magic number' which we can live with if it must be hardcoded, although it would be better coming from a configuration file or setting. The bigger problem is that unless you are an employee of HEXIE Information technology Co., Ltd who own that address block then stick to assigning to number ranges reserved for 'private use' like 192.168.0.0/16 10.0.0.0/8 for example which give more than enough scope.
if (!(bool) objMO["IPEnabled"]) {
Is there a way to refactor this to make it clearer?
Looking at the performance issue you have invoking EnableStatic
you might want to look at StackOverflow: Problems using the WMI EnableStatic method
objMO.InvokeMethod("EnableStatic", objNewIP, null);
is slowly. \$\endgroup\$