As part of a NuGet installation we need to open a web page when the installation completes.
Our requirement is to:
- Open web page in Visual Studio in a new window if the page is not already open in another window.
Opening a web page by itself is straight forward:
... DTE.ItemOperations.Navigate(WebPageUri.ToString(), EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow); ...
The problem has been finding a way to get a list of the currently open web pages, so that we can decide if a page needs to be opened.
I am certain the solution is simple and has been staring us in the face all along.
How do we get a list of the currently open web pages in Visual Studio using EnvDTE?
A solution to the problem is listed below. I doubt the approach is a best practice, but it does appear to work.
Web browser windows in Visual Studio have an “ObjectKind” value of “EnvDTE.Constants.vsWindowKindWebBrowser”. The “Object” value is the Internet Explorer browser and implements “IWebBrowser2”. Although “IWebBrowser2” is not .Net, “System.Windows.Forms.WebBrowser” is (apparently) a wrapper around “IWebBrowser2”. “System.Windows.Forms.WebBrowser” has an “AttachInterfaces” method which can be overridden, which allows us to pass the window object and let “System.Windows.Forms.WebBrowser” do the heavy lifting.
The end result is:
A list of open web pages is then found by: