8.1.12

Get Web Browser History in C# - Google Chrome

This post is a sort-of sequel to my previous blog post on getting web browser history in C#. Google Chrome is the browser I had missed in the last blog post and I must say, it was quite interesting to code this up. Just like Firefox, Chrome also uses an SQLite Database, but uses a structure quite different than that of Firefox. More details about it here. As for Firefox, a very nice article is here. Before you see the code, below are a few interesting points to note about Chrome:
  1. The History file for Chrome is an SQLite database, but without the .sqlite extension. In fact, it does not have any extension. Its name is just History.
  2. Google Chrome stores last visited times in UTC format. UTC time is basically the time elapsed since Jan 1, 1601. Unlike the UTC format for Windows file times which is in nanoseconds, Chrome stores the times in microseconds.
  3. Also, the times are as per the GMT time zone. So, to get local times, TimeZoneInfo class provided by the .NET framework must be used.
Below is a tiny code snippet for getting Chrome history in C#:



  1. public class HistoryItem
  2. {
  3. public string URL { get; set; }
  4. public string Title { get; set; }
  5. public DateTime VisitedTime { get; set; }
  6. }
  7. static class Program
  8. {
  9. static void Main()
  10. {
  11. string chromeHistoryFile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
  12. + @"\Google\Chrome\User Data\Default\History";
  13. if (File.Exists(chromeHistoryFile))
  14. {
  15. SQLiteConnection connection = new SQLiteConnection
  16. ("Data Source=" + chromeHistoryFile + ";Version=3;New=False;Compress=True;");
  17. connection.Open();
  18. DataSet dataset = new DataSet();
  19. SQLiteDataAdapter adapter = new SQLiteDataAdapter
  20. ("select * from urls order by last_visit_time desc", connection);
  21. adapter.Fill(dataset);
  22. if (dataset != null && dataset.Tables.Count > 0 & dataset.Tables[0] != null)
  23. {
  24. DataTable dt = dataset.Tables[0];
  25. allHistoryItems = new List<HistoryItem>();
  26. foreach (DataRow historyRow in dt.Rows)
  27. {
  28. HistoryItem historyItem = new HistoryItem();
  29. {
  30. URL = Convert.ToString(historyRow["url"]),
  31. Title = Convert.ToString(historyRow["title"])
  32. };
  33. // Chrome stores time elapsed since Jan 1, 1601 (UTC format) in microseconds
  34. long utcMicroSeconds = Convert.ToInt64(historyRow["last_visit_time"]);
  35. // Windows file time UTC is in nanoseconds, so multiplying by 10
  36. DateTime gmtTime = DateTime.FromFileTimeUtc(10 * utcMicroSeconds);
  37. // Converting to local time
  38. DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(gmtTime, TimeZoneInfo.Local);
  39. historyItem.VisitedTime = localTime;
  40. allHistoryItems.Add(historyItem);
  41. }
  42. }
  43. }
  44. }
  45. }