elefantsko
elefantsko

Reputation: 33

Download the html source code to a string from a open website in IE with C#?

I been looking all over for this answer but can't find it anywhere.. This is what I want to be able to do:

I have a form application where i have a button that says "collect html code". When I press this button I want C# to download the HTML source code from the website I'm currently on (using IE). I've been using this code:

WebClient web = new WebClient();
string html = web.DownloadString("www.example.com");

But now I don't want to specify the URL in my code! And I don't want to use a webbrowser in my application. Anyone got a solution? Thanks!

Upvotes: 0

Views: 503

Answers (2)

user2440074
user2440074

Reputation:

You are talking about getting URLs from IE window? If so, here you are:

var urls = (new SHDocVw.ShellWindows()).Cast<SHDocVw.InternetExplorer>().
                Select(x => x.LocationURL).ToArray();

Don't forget to add COM reference "Microsoft Internet Controls" in your project.

Upvotes: 0

Saman Gholami
Saman Gholami

Reputation: 3512

With this code you can get IE7 and later version URL in opened tabs :

SHDocVw.ShellWindows allBrowsers = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer ieInst in allBrowsers )
    {
        String url = ieInst.LocationURL;
        // do your stuff
    }

So you can access the urls and do your stuff with WebClient class.

You need to add a reference to a COM component called Microsoft Internet Controls

Upvotes: 1

Related Questions