0

enter image description here

I am writing an application to make changes to network adapter IP address settings. Only the basic IP settings is what I will change from the application.

Is there any way for me to make use of some sort of "link" or "path" to open the "Advanced TCP/IP Settings" screen normally accessed VIA the "Advanced" button in the TCP/IPv4 Properties screen?

The above screen capture shows the highlighted button that would normally be used to open the advanced TCP/IP Settings screen. I need a way to open this exact screen from my application directly using a button.

Solx85
  • 244
  • 2
  • 16

2 Answers2

1

You could do something like:

System.Diagnostics.Process.Start("ncpa.cpl"); // opens network connections window
Thread.Sleep(500); // give time for window to open
SendKeys.Send("(^a){RIGHT}(%f)r"); // select all (ctrl+a), right arrow, alt+f, r
Loathing
  • 5,109
  • 3
  • 24
  • 35
  • I dont think this is the right way of doing this.. You should be using WMI to achieve this.. Look at my answer for a sample of doing this. – Vishweshwar Kapse Jan 02 '15 at 11:06
  • Please see the edit of the question as I have now added a screen shot of the exact screen that I need to open. Regards. – Solx85 Jan 03 '15 at 12:05
  • 1
    I've given you the basic idea how to do it for a quick and dirty solution. You can figure out the rest of the keystrokes. You can also use the .Net UI Automation library to add more consistency. Here is a really good starting point for UI Automation: http://stackoverflow.com/questions/4665045/how-to-get-the-word-under-the-cursor-in-windows – Loathing Jan 03 '15 at 12:47
  • @Loathing where can I find a reference to the command ncpa.cpl, there might be other similar that I can use somewhere? – Solx85 Jan 04 '15 at 09:41
0

You can set the IP address of a network adapter by using the following code

public void setIP(string IPAddress,string SubnetMask, string Gateway) 
{ 

ManagementClass objMC = new ManagementClass(
"Win32_NetworkAdapterConfiguration"); 
ManagementObjectCollection objMOC = objMC.GetInstances(); 


foreach(ManagementObject objMO in objMOC) 
 {     

  if (!(bool) objMO["IPEnabled"]) 
       continue; 



  try 
    { 
      ManagementBaseObject objNewIP = null; 
      ManagementBaseObject objSetIP = null; 
      ManagementBaseObject objNewGate = null; 


      objNewIP = objMO.GetMethodParameters("EnableStatic"); 
      objNewGate = objMO.GetMethodParameters("SetGateways"); 



      //Set DefaultGateway
      objNewGate["DefaultIPGateway"] = new string[] {Gateway}; 
      objNewGate["GatewayCostMetric"] = new int[] {1}; 


      //Set IPAddress and Subnet Mask
      objNewIP["IPAddress"] = new string[] {IPAddress}; 
      objNewIP["SubnetMask"] = new string[] {SubnetMask}; 

      objSetIP = objMO.InvokeMethod("EnableStatic",objNewIP,null); 
      objSetIP = objMO.InvokeMethod("SetGateways",objNewGate,null); 



      Console.WriteLine(
         "Updated IPAddress, SubnetMask and Default Gateway!"); 



    } 
    catch(Exception ex) 
    { 
          MessageBox.Show("Unable to Set IP : " + ex.Message); } 
    }
Vishweshwar Kapse
  • 921
  • 6
  • 23
  • 43
  • 2
    but this is not actually OP wants – Vivek S. Jan 02 '15 at 11:10
  • 1
    Solx85 you can write your own windows form with a few Text boxes and a button on click of the button You can use this code for updating the IP... I feel this is a better way of doing it @mr.incognito because lothing's answer is sending KeyStrokes to get this done, Tomorrow if Microsoft Changes the UI of the ncpa.cpl (Network adapters Control Panel) then his code will no longer work – Vishweshwar Kapse Jan 02 '15 at 11:14
  • I use the Win32_NetworkAdapter class to manage all IP related settings. The application is meant for simple and basic IP settings but I have now started to use the advanced settings more frequently to add multiple IP addresses to an adapter. I only want to open the specific screen by the click of a button in my application. I will try the code below and provide feedback. – Solx85 Jan 03 '15 at 11:57