Pages

Ads 468x60px

Friday, June 29, 2012

How to access Internet Explorer History in C#

So, you want to know how to access history for various browsers in C#? I found it very difficult to get it working correctly, so here is my top choices for Chrome, Firefox, and IE history code. Internet Explorer First off, here is IE. IE is, I think, the easiest history to be obtained. Browser history for IE is stored in the registry key of HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs. Now, Microsoft has published an interface on IUrlHistory, however that was not a C# wrapper. So, an article on CodeProject has a UrlHistoryLibrary.dll file which allows access to read and modify IE history.  The article shows how to to use the whole interface, however here is some prewritten code for returning IE history:
public class IE
    {
        public IEnumerable<URL> GetHistory()
        {

            UrlHistoryWrapperClass urlHistory;
            UrlHistoryWrapperClass.STATURLEnumerator enumerator;
            List<STATURL> list = new List<STATURL>();

            urlHistory = new UrlHistoryWrapperClass();
            enumerator = urlHistory.GetEnumerator();
            enumerator.GetUrlHistory(list);

            List<URL> URLs = new List<URL>();

            foreach (STATURL url in list)
            {
                string browser = "-- Internet Explorer";

                bool containsBoth = url.URL.Contains("file:///");

                if (url.Title != "" && containsBoth == false){
                    Console.WriteLine(String.Format("{0} {1} {2}", url.Title, url.URL, browser));
                    Thread.Sleep(500);
                    }

            }
            return URLs;
        }
    }
Here is the URL class:
public class URL
{
string url;
string title;
string browser;
public URL(string url, string title, string browser)
{
this.url = url;
this.title = title;
this.browser = browser;
}
}
 Here is how you use this code:
  1. Go to the Codeproject page, and download the demo project. You may have to sign up for a free account. Now, open up the "UrlHistoryLibrary" project in Visual Studio and then build it. You should now have a "UrlHistoryLibrary.dll" file in /bin/debug.
  2. Now, create a new console project in Visual Studio. I use the 2010 Pro version.
  3. Right click the "References" folder, in the Solution Explorer, and choose the option to "Add Reference". Now, select the "Browse" folder and navigate into your "UrlHistoryLibrary/Bin/Debug" folder and select "UrlHistoryLibrary.dll". 
  4. Now, in your references, add 
    using UrlHistoryLibrary;
    using System.Diagnostics;
    using System.Threading;
  5. Now, add the above IE class to your code. Right under that, outside of the IE class, add the URL class.  
  6. Add a main class, or public static void Main(string[] args). Inside of that, include the following:
    Process[] process_IE = Process.GetProcessesByName("iexplore");
    
    if (process_IE.Length == 0)
                {
                    IE ie = new IE();
    
                    ie.GetHistory();
                }
                else
                    Console.WriteLine("Sorry, IE is open. Please close IE and try again.");
  7. Now, when you run your console program, you should get a list of History visited, in the format of Title, Url, Broswer.
A few notes:
  • I have modified this a bit from the original code. I have made it so that the results will not display results with "file:///" in the url. It will also not display any empty titles.
  • In the main function, I have added a check that will tell you if you have IE open, when you try to run. If so, it will give you an error message.
  • I have added a 5 second delay between each URL.
Here is the complete tested source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using UrlHistoryLibrary;


namespace BlogTestIE
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] process_IE = Process.GetProcessesByName("iexplore");

            if (process_IE.Length == 0)
            {
                IE ie = new IE();

                ie.GetHistory();
            }
            else
                Console.WriteLine("Sorry, IE is open. Please close IE and try again.");

        }
    }

    public class IE
    {
        public IEnumerable GetHistory()
        {

            UrlHistoryWrapperClass urlHistory;
            UrlHistoryWrapperClass.STATURLEnumerator enumerator;
            List list = new List();

            urlHistory = new UrlHistoryWrapperClass();
            enumerator = urlHistory.GetEnumerator();
            enumerator.GetUrlHistory(list);

            List URLs = new List();

            foreach (STATURL url in list)
            {
                string browser = "-- Internet Explorer";

                bool containsBoth = url.URL.Contains("file:///");

                if (url.Title != "" && containsBoth == false)
                {
                    Console.WriteLine(String.Format("{0} {1} {2}", url.Title, url.URL, browser));
                    Thread.Sleep(500);
                }

            }
            return URLs;
        }
    }

    public class URL
    {
        string url;
        string title;
        string browser;
        public URL(string url, string title, string browser)
        {
            this.url = url;
            this.title = title;
            this.browser = browser;
        }
    }
}

6 comments:

  1. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.


    Microsoft Internet Explorer Support

    ReplyDelete
  2. I try to use it in visual web part and deploy it in sharepoint site
    and i use windows server 2008 enterprise R2
    visual studio 2010
    error appear in
    urlhistory=new UrlHistoryClass(); line.
    SecurityException was unhandled by user code
    System.security.Permission.SecurityPermission
    Thanks

    ReplyDelete
  3. Hi. i try to run this code but its giving me error on the "url" word i.e.
    "Error :The type or namespace name 'URL' could not be found (are you missing a using directive or an assembly reference?)"
    plz can u help me with this error its urgent...

    ReplyDelete
    Replies
    1. Did you include the URL class I gave you?

      Delete