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

Posted by OmegaMan at June 21, 2007

Category: How To

Tags: ,

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:

image

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   : http://www.omegacoder.com/

Share

30 Comments

  1. vpalle says

    In IE7, is it possible to get the url of the current active tab, the above code gets the url of the tab that was created last.

    Reply
  2. omegaman says

    I need to research that…

    Reply
  3. vitalikaz says

    Hi,

    I need to learn the URL adress of the ACTIVE window, when I already know the title of this window, the process name, process ID etc. Unfortunately, I can’t learn which window is currently active, when using SHDocVw.ShellWindowsClass(). Help plz 🙁

    Reply
  4. omegaman says

    Vitalikaz..post an explanation of your problem to the MSDN forums specifically the Visual C# General and a code snippet of what you are doign. That would be the best place to find an answer. I will look for it on there. Thanks!

    Reply
  5. vitalikaz says

    Ok, I’ll try. Thank you!

    Reply
  6. Bishwajeet says

    After using ShellWindows Class in the code , will it work for Vista.

    If anyone know any way to get it work in vista.

    Help me!!

    Reply
  7. DBS says

    Can’t use this in vista? can u help me???

    Reply
  8. Ivan says

    That works for me even with IE 6. Thanks a lot

    Reply
  9. simsmaster says

    Does not work on Vista so far…

    Reply
  10. omegaman says

    Works on my copy of Vista and returns all the tabs in IE8. I surmise any problems in Vista are due to the UAC/permissions of the user. I am admin on my box with UAC set to a moderate level. HTH

    Reply
  11. Isha Khanna says

    nice! works like a charm 🙂

    Can i please know a way to refresh a page in IE?

    should I use process.diagnostics, or do i need to use WebBrowser methods? have read a lot of things, but understood none :p

    Thanks!

    Reply
  12. Arvind says

    Its very simple just use refresh method of SHDocVw.InternetExplorer

    Reply
  13. imran says

    thnkx for ur code!!!
    it was really helpful

    it works fine with ie,
    but can u help me to fetch url from mozila!!!

    bye thankx!

    Reply
  14. balaji says

    Thanks for your code.

    The above coding is working fine.

    But i need to fetch the url from mozilla and safari browser.

    Reply
  15. omegaman says

    If I knew how to get it from other browsers I would post it as a blog. Sorry guys.

    Reply
  16. VENU says

    hi Musings
    this is very good article.

    But i need to fetch the url from mozilla and safari browser.

    Reply
  17. Ihtesham says

    The above code will work say in a service which is logging the number of IE windows count to a text file. If you switch the user on windows and open the log created by the service, the shellWindows.count would have returned 0, regardless of any opened browsers by this user account.

    Reply
  18. Ihtesham says

    [DllImport(“user32.Dll”)]
    public static extern int EnumWindows(CallBack x, int y);
    [DllImport(“User32.Dll”)]
    public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
    [DllImport(“User32.Dll”)]
    public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);
    [DllImport(“User32.Dll”)]
    public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

    Reply
  19. ihtesham says

    The code is fine as long as it is running as a windows form or console app. But if you wrap this code under a windows service it throws an ugly exception saying

    Retrieving the COM class factory for component with CLSID {A14C86FD-F567-44CF-8C82-C3DB3AFA6570} failed due to the following error: 8007000e.

    I am really struggling with this. Can some one help please…

    Reply
  20. Shahnoor says

    hi wow it is so easy, but i have a problem. I have different instances of internet explorers running, and some runnning as different accounts and (SHDocVw.InternetExplorer ie in WindowsShell) only gets me the instances running with the logged user only. Is there some way to get the other runas instances of internet explorer?

    Reply
  21. Ram says

    Hi,

    I am working on a app we have deployed this in xenapp 6 citrix server running on windows 2008 r2 64 bit when we create a object of ShDocVw.ShellWindows sw = new ShellWindows();

    we will get the following error

    Retrieving the COM class factory for component with CLSID {9bA05972-F6A8-11CF-A442-00A0C90A8F39} failed due to the following error: 80040154

    I tried to register the ShDocVw dll but we were not successfull pls help.

    Reply
  22. Gouri says

    it is working fine.thanks a lot for this.
    But i was at one point.
    Suppose if open multiple IE,then how can i fetch the active IE url.

    Reply
  23. İbrahim Emür says

    Hi.

    Thank you.

    Nice example.

    Reply
  24. Dan says

    I found that the code wont wok for terminal users sessions running in published application mode and not desktop mode. (as in Citrix Xenapp or Microsoft Session Manager) and the reason is that in those sessions the explorer.exe is not running.

    I’m looking for a way to use the code in such sessions where explorer.exe process is not running.

    Thanks.

    Reply
  25. satyendra says

    nice example .If i opened multiple tabs in ie, then how cud we retrieve all those list of urls??

    Reply
  26. Bob Thompson says

    This is an excellent article! The code is perfect for what I want and it works like a charm in Windows 7. Before I struggled with “EnumWindows” and couldn’t get it to work reliably. But with this code I don’t need to fuss with that anymore.

    Reply
  27. Sam says

    You just saved me a huge headache. I can finally complete my application the way I really want, without using other insane methods that I (unfortunately already) implemented.

    Keep up the great posts!

    Reply
  28. giorgio says

    Thanks for the post. It works for me, I just have a follow up question: How can I get the location when the explorer is looking at “My Computer” and “My Network Places”? It just returns an empty string. Perhaps it’s because these locations are not physical paths? Is there a way to get it right?

    Reply
  29. Manas says

    Thanks a lot for the code!!
    It was exactly what i was looking for

    Reply
  30. Jerome says

    Asking questions are in fact fastidious thing if you
    are not understanding anything completely, but this piece of writing gives pleasant understanding even.

    Reply

Leave a comment

(required)
(required) (will not be published)

This site uses Akismet to reduce spam. Learn how your comment data is processed.