2
\$\begingroup\$

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?

Mooseman
1993 silver badges12 bronze badges
asked Oct 25, 2014 at 23:18
\$\endgroup\$
4
  • \$\begingroup\$ Can you debug the code on another computer with XP? If so, what is the slowest line in the code? \$\endgroup\$ Commented Oct 26, 2014 at 0:19
  • \$\begingroup\$ Maybe Microsoft optimized the feature in windows 7. \$\endgroup\$ Commented Oct 26, 2014 at 7:50
  • 1
    \$\begingroup\$ @Dmitry > objMO.InvokeMethod("EnableStatic", objNewIP, null); is slowly. \$\endgroup\$ Commented Oct 26, 2014 at 8:31
  • \$\begingroup\$ Check out this app. It sets the ips of both wifi and ethernet. Also highlights current configuration with cyned coloured buttons. github.com/kamran7679/ConfigureIP \$\endgroup\$ Commented Jan 24, 2017 at 13:40

2 Answers 2

4
\$\begingroup\$

Can't help with the speed question but some generic remarks:

  1. Both ManagementClass and ManagementObjectCollection are IDisposable and should therefore be wrapped in a using block to make sure any unmanaged resources are properly released.

  2. 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.

  3. In C# land the common naming convention for methods is PascalCase and not camelCase. 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.

answered Oct 26, 2014 at 1:21
\$\endgroup\$
0
2
\$\begingroup\$

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

answered Oct 26, 2014 at 13:24
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.