Posts tagged ‘Internet Explorer’

Access Explorer and Internet Explorer in C# to Find Web Pages and Directories

In this article I describe how to get the current URL in Internet Explorer and the current directory in Windows Explorer. This works in .Net 2 and IE 7. I am showing both at the same time because they are similar in methods of getting the address of the items, whether its a hard drive or a URL.

First step requires the access ShellWindows object which represents a collection of open windows in the system. To access that object which resides in the SHDocVw namespace we need to import a Com library of `Microsoft Internet Controls` in to the project:


With that we can access the ShellWindows object and begin our work. Here is the code sample

using System.IO;


SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

string filename;

foreach ( SHDocVw.InternetExplorer ie in shellWindows )
    filename = Path.GetFileNameWithoutExtension( ie.FullName ).ToLower();

    if ( filename.Equals( "iexplore" ) )
        Console.WriteLine( "Web Site   : {0}", ie.LocationURL );

    if ( filename.Equals( "explorer" ) )
        Console.WriteLine( "Hard Drive : {0}", ie.LocationURL );

Thanks to the magic of ShellWindows we are able to see all active windows. We discriminate and get the names of the windows we are interested in and voila here is the output:

Hard Drive : file:///C:/Work/Net2
Web Site   :