vyclarks
vyclarks

Reputation: 878

Open Local Area Network (LAN) Settings window by code

I have this code:

public void OpenInterOption()
{
    System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
    proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
    proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";
    System.Diagnostics.Process.Start(proc);
}

The result is Internet Properties window opened.
But actually I want to open "Local Area Network (LAN) Settings Window" in the Internet Properties tab.
I think the problem in this line:"shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4"; Does it need more arguments?

Upvotes: 1

Views: 3734

Answers (1)

Edper
Edper

Reputation: 9322

I don't know if opening the network settings would be the right way to go, because if you have more than one LAN including wireless ones which one you want to open? So, you better off opening the Network Connections settings instead and let the user decide which one to open. Thus you could use the code below to open Network Connection settings like:

ProcessStartInfo startInfo = new ProcessStartInfo("NCPA.cpl");
startInfo.UseShellExecute = true;

Process.Start(startInfo);

Update:

You could not call directly LAN Property Settings using any .cpl program. However, there is an unconventional way using your own code and by using SendKeys like this:

   System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
   proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
   proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";
   System.Diagnostics.Process.Start(proc);
   SendKeys.Send("{TAB}{TAB}{TAB}{TAB}{ENTER}");

Another way is to use Alt+L instead of Tab. Now, for me this is more sure because in Tab you never know if all will be registered right away or it hops to exactly number of locations like buttons. However, I have to use Timer in order to make sure that it will really effect like this:

   tmr.Interval = 500;

   System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
   proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
   proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";
   System.Diagnostics.Process.Start(proc);
   tmr.Tick += new EventHandler(tmr_Tick);
   tmr.Start();

And your Event handler for your Timer:

   void tmr_Tick(object sender, EventArgs e)
   {
       SendKeys.SendWait("%L");
       tmr.Stop();    
   }

Now, make sure that you declare the Timer as global in your class whether inside a form or something else like:

   Timer tmr = new Timer();

Like in my case I put it under and inside Form1 class like:

  public partial class Form1 : Form
  {
    Timer tmr = new Timer();
  .....more code here not shown

Not the most elegant but it will get the job done ;-)

Upvotes: 3

Related Questions