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 : http://www.omegacoder.com/
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.
I need to research that…
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 :(
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!
Ok, I’ll try. Thank you!
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!!
Can’t use this in vista? can u help me???
That works for me even with IE 6. Thanks a lot
Does not work on Vista so far…
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
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!
Its very simple just use refresh method of SHDocVw.InternetExplorer
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!
Thanks for your code.
The above coding is working fine.
But i need to fetch the url from mozilla and safari browser.
If I knew how to get it from other browsers I would post it as a blog. Sorry guys.
hi Musings
this is very good article.
But i need to fetch the url from mozilla and safari browser.
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.
[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);
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…
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?
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.
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.
Hi.
Thank you.
Nice example.
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.
nice example .If i opened multiple tabs in ie, then how cud we retrieve all those list of urls??
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.
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!
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?
Thanks a lot for the code!!
It was exactly what i was looking for
Asking questions are in fact fastidious thing if you
are not understanding anything completely, but this piece of writing gives pleasant understanding even.
Thanks for this simple yet delightful code.