Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch a URL in a tab in an existing IE window from C#

The following code opens a link in an existing browser window when browserExe is pointing to Firefox, Safari or Chrome. When pointing to IEXPLORE.EXE (IE7) a new windows is opened.

ProcessStartInfo pi = new ProcessStartInfo(browserExe, url);
Process.Start(pi);

This opens a tab in an existing window as intended, when IE is the default browser.

ProcessStartInfo pi = new ProcessStartInfo(url);
Process.Start(pi);

How to i reuse an existing IE windows, when IE is NOT the default browser?

like image 346
Fedearne Avatar asked Sep 14 '10 21:09

Fedearne


People also ask

How to open a link in a new tab?

If you want to open the page in a new tabHold down the Ctrl key and click the link. Use the wheel button of your mouse to click the link. Right-click the link and then click Open link in new tab.

How do I get Internet Explorer to open a new tab when I click on a link?

The fastest method to open a new tab in all browsers is to press the shortcut key combination Ctrl + T (PC users) or Command + T (Mac users).


1 Answers

Using shdocvw library (add reference to it, you can find it in windows\system32) you can get the list of instances and call navigate with the newtab parameter:

ShellWindows iExplorerInstances = new ShellWindows();
if (iExplorerInstances.Count > 0)
{
  IEnumerator enumerator = iExplorerInstances.GetEnumerator();
  enumerator.MoveNext();
  InternetExplorer iExplorer = (InternetExplorer)enumerator.Current;
  iExplorer.Navigate(url, 0x800); //0x800 means new tab
}
else
{
  //No iexplore running, use your processinfo method
}

Edit: in some cases you may have to check if the shellwindow corresponds to a real iexplorer an not to any other windows shell (in w7 all instances are returned, don't know now for others).

   bool found=false;
   foreach (InternetExplorer iExplorer in iExplorerInstances)
   {
       if (iExplorer.Name == "Windows Internet Explorer")
       {
           iExplorer.Navigate(ur, 0x800);
           found=true;
           break;
       }
   }
   if(!found)
   {
      //run with processinfo
   }

You may also find these additional IE Navigate Flags useful. Full description of the flags are available at http://msdn.microsoft.com/en-us/library/dd565688(v=vs.85).aspx

enum BrowserNavConstants 
{ 
    navOpenInNewWindow = 0x1, 
    navNoHistory = 0x2, 
    navNoReadFromCache = 0x4, 
    navNoWriteToCache = 0x8, 
    navAllowAutosearch = 0x10, 
    navBrowserBar = 0x20, 
    navHyperlink = 0x40, 
    navEnforceRestricted = 0x80, 
    navNewWindowsManaged = 0x0100, 
    navUntrustedForDownload = 0x0200, 
    navTrustedForActiveX = 0x0400, 
    navOpenInNewTab = 0x0800, 
    navOpenInBackgroundTab = 0x1000, 
    navKeepWordWheelText = 0x2000, 
    navVirtualTab = 0x4000, 
    navBlockRedirectsXDomain = 0x8000, 
    navOpenNewForegroundTab = 0x10000 
};
like image 51
jmservera Avatar answered Oct 02 '22 19:10

jmservera