Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I did a code to get current ie URL from ie browser
C#
foreach (InternetExplorer ie in new ShellWindows())
{
   label1.Text = ie.LocationURL.ToString();
   label2.Text = ie.LocationName.ToString();
}

but it showing last opened URL instead of active URL.
So, how do I modify my code to get current active URL from ie browser?
Thanks.


[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
BELGIUMsky 25-Apr-14 8:20am    
i don't see anything wrong with this code.
what is your timetable for testing?

like:
fist open page in ie, then run program, then type in url in ie, ... ?
Priyanka Bhawsar 28-Apr-14 4:11am    
Thanks to reply me. yes process is same but my problem is, suppose if I open 10 tabs in ie browser and then I open 2nd number's tab then still it shows the URL of 10th no.'s tab in label1.Text(last tab's URL).
BELGIUMsky 28-Apr-14 5:21am    
oh you don't want every open page you want the page the user sees?
will check if i can find that
Priyanka Bhawsar 28-Apr-14 6:22am    
Yes, You are right, please help me if you will find that. Thanks
BELGIUMsky 28-Apr-14 6:36am    
what internet explorer version do you use? i have 11 atm
http://www.nullskull.com/q/10112922/how-to-get-current-active-tab-handle-in-an-ie7-window-using-c.aspx

and tried the first method but its not working for me.

There is no such thing as an "Active URL". Every browser window can only navigate to one URL, and every window is the "Active URL".

What you're referring to is which window has the input focus. The ShellWindow class does not track that.

You can use the GetForegroundWindow[^] function to get the handle to the window that has the input focus. You can then compare that handle to the window handles you get if you enumerate over the list of processes you get from calling Process.GetProcessesByName() [^]and look for "iexplorer.exe".
 
Share this answer
 
Comments
Priyanka Bhawsar 28-Apr-14 4:41am    
Yes, I know that every browser window can only navigate to one URL but my problem is, suppose if I open 10 tabs in ie browser and then I open 2nd number's tab then still it shows the URL of 10th no.'s tab in label1.Text(last tab's URL).
Dave Kreskowiak 28-Apr-14 7:27am    
I'll say it again: there is no active URL. Just because you have one tab selected in IE says nothing about it being "active". It merely has the input focus and the ShellWindow does not track that.
 
Share this answer
 
Comments
Priyanka Bhawsar 28-Apr-14 4:40am    
Yes, I did the same but I am not getting the value of current active tab URL.
[no name] 28-Apr-14 4:59am    
what u are getting? any eroor ??are u missing any reference ?
Priyanka Bhawsar 28-Apr-14 6:20am    
I am not getting error. I did that code which written in answer but its showing last opened URL value instead of current active URL.
C#
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 );
}
 
Share this answer
 
Comments
Priyanka Bhawsar 28-Apr-14 6:17am    
Thanks to answer me, but I have also tried this, its showing last opened URL instead of current active URL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900