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. }

33 comments:

  1. Hi
    I tried this code to capture the google chrome browser history but I am getting this error as "The database file is locked
    database is locked"
    could you please suggest me how to fix this issue.
    I have installed sqlite also

    ReplyDelete
    Replies
    1. Make sure you are not running chrome.

      Delete
    2. Yes, this code cannot run when Chrome is running at the same time

      Delete
  2. when I am running the above code i am getting this error
    "Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information."
    Is it because of some version problem? plz help..

    swati

    ReplyDelete
  3. please help me with the above issue its urgent..

    swati

    ReplyDelete
    Replies
    1. Sorry for the late reply... but I think it is because you are using .NET 4.0. At the time of publishing this article, I think .NET 4.0 was not developed yet. Check the SQLite website to see if there is an assembly built for .NET 4.0. Hope this helps.

      Delete
  4. Dear Joel
    i am using Vb 6.0 to get browsing history. Is it possible to get history in vb

    ReplyDelete
    Replies
    1. Hey, Pravin! I wouldn't be able to help you on that. I searched it up and got a link which you might find helpful: http://www.bigresource.com/VB-Clear-IE-Browser-Cache-and-History-with-VBA-3HSDDKjzOB.html

      Delete
  5. can you provide a vb.net solution please. urgent!

    ReplyDelete
    Replies
    1. Hi Hassan, please use this cool tool to convert from C# to VB: http://www.developerfusion.com/tools/convert/csharp-to-vb/

      Delete
  6. Please let me know if anyone know other way to get history when browser is running...its too urgent!!!!

    ReplyDelete
    Replies
    1. This should not be possible since the database file is exclusively locked by the browser process when it is running. It may have been made possible though recently.. you just have to Google hard enough :)

      Delete
  7. when i use the above given code it gives me "Unable to load DLL 'sqlcrypt3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" error while i followed the same process as you have explained.
    Please let me know...???
    Thanks
    samee

    ReplyDelete
    Replies
    1. You need to download ado.net provider for sqlite. Details have been provided in my earlier post for Firefox, but here is the link: http://sourceforge.net/projects/sqlite-dotnet2/

      Delete
  8. Can we stop chrome for some time till we get the history than continue running after ward and how it could be done if possible?

    ReplyDelete
    Replies
    1. Hi samee, i think you are suggesting to suspend the app.. I don't think that's possible, but you could google around

      Delete
  9. connection string error occur please help me how to solve it ?

    ReplyDelete
  10. hey Joel Mathias,

    i closed chrome and even log out from my gmail account,but it work only on localhost,i.e , on my website it not work,i don't get any error notification but it not work either,any suggestions?

    ReplyDelete
    Replies
    1. You mean your code is running on web server and you want the client's web browser history? This code is not meant for that. This code gets the web browser history of the machine it is executed in.

      Delete
  11. Error 1 Cannot implicitly convert type 'ConsoleApplication1.HistoryItem' to 'string'
    Error 2 'string' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
    this 2 error will come please help me ..

    ReplyDelete
    Replies
    1. Try adding this statement at the top of your code file:

      using System.Collections.Generic;

      Delete
    2. Thanks joel
      then this two error will come
      Error 1 The name 'allHistoryItems' does not exist in the current context

      Error 2 The name 'allHistoryItems' does not exist in the current context

      Delete
    3. With all due respect, I think you need to learn some programming.

      Delete
  12. hi joel,
    I am including browser history randomely in my project.. where the search history is need to be stored in my MySQL database table and displayed whenever user clicks history.. i searched allot but all are related to c#, vb,vs etc..finally i saw this blog but my code is entirely in jsp.. plz help me its very urgent.. if possible mail to shahaskhan123@gmail.com.. plzzzz... tanqs in advance.. if u don't understand my query plz mail..

    ReplyDelete
  13. your code not working on google crome .Please help me

    ReplyDelete
  14. Hai sir,
    Can u please help me in recovering deleted Firefox web browser history?

    ReplyDelete
  15. Can i get history of google chrome's incognito mode browser by c#.

    ReplyDelete
  16. can you provide a Web application code for fetching history of web browser please

    ReplyDelete
  17. is it possible to get records which we deleted records with cookies

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Hi....without using Hardcode Path :- (+ @"\Google\Chrome\User Data\Default\History") lines; can we directly take the path from code itself using c#.net

    ReplyDelete