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:
- 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.
- 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.
- 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#:
- public class HistoryItem
- {
- public string URL { get; set; }
- public string Title { get; set; }
- public DateTime VisitedTime { get; set; }
- }
- static class Program
- {
- static void Main()
- {
- string chromeHistoryFile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
- + @"\Google\Chrome\User Data\Default\History";
- if (File.Exists(chromeHistoryFile))
- {
- SQLiteConnection connection = new SQLiteConnection
- ("Data Source=" + chromeHistoryFile + ";Version=3;New=False;Compress=True;");
- connection.Open();
- DataSet dataset = new DataSet();
- SQLiteDataAdapter adapter = new SQLiteDataAdapter
- ("select * from urls order by last_visit_time desc", connection);
- adapter.Fill(dataset);
- if (dataset != null && dataset.Tables.Count > 0 & dataset.Tables[0] != null)
- {
- DataTable dt = dataset.Tables[0];
- allHistoryItems = new List<HistoryItem>();
- foreach (DataRow historyRow in dt.Rows)
- {
- HistoryItem historyItem = new HistoryItem();
- {
- URL = Convert.ToString(historyRow["url"]),
- Title = Convert.ToString(historyRow["title"])
- };
- // Chrome stores time elapsed since Jan 1, 1601 (UTC format) in microseconds
- long utcMicroSeconds = Convert.ToInt64(historyRow["last_visit_time"]);
- // Windows file time UTC is in nanoseconds, so multiplying by 10
- DateTime gmtTime = DateTime.FromFileTimeUtc(10 * utcMicroSeconds);
- // Converting to local time
- DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(gmtTime, TimeZoneInfo.Local);
- historyItem.VisitedTime = localTime;
- allHistoryItems.Add(historyItem);
- }
- }
- }
- }
- }
Hi
ReplyDeleteI 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
Make sure you are not running chrome.
DeleteYes, this code cannot run when Chrome is running at the same time
Deletewhen I am running the above code i am getting this error
ReplyDelete"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
please help me with the above issue its urgent..
ReplyDeleteswati
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.
DeleteDear Joel
ReplyDeletei am using Vb 6.0 to get browsing history. Is it possible to get history in vb
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
Deletecan you provide a vb.net solution please. urgent!
ReplyDeleteHi Hassan, please use this cool tool to convert from C# to VB: http://www.developerfusion.com/tools/convert/csharp-to-vb/
DeletePlease let me know if anyone know other way to get history when browser is running...its too urgent!!!!
ReplyDeleteThis 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 :)
Deletewhen 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.
ReplyDeletePlease let me know...???
Thanks
samee
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/
DeleteCan we stop chrome for some time till we get the history than continue running after ward and how it could be done if possible?
ReplyDeleteHi samee, i think you are suggesting to suspend the app.. I don't think that's possible, but you could google around
Deleteconnection string error occur please help me how to solve it ?
ReplyDeletehey Joel Mathias,
ReplyDeletei 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?
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.
DeleteError 1 Cannot implicitly convert type 'ConsoleApplication1.HistoryItem' to 'string'
ReplyDeleteError 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 ..
Try adding this statement at the top of your code file:
Deleteusing System.Collections.Generic;
Thanks joel
Deletethen 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
With all due respect, I think you need to learn some programming.
Deletehi joel,
ReplyDeleteI 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..
Can't help you with JSP, buddy. Sorry!
Deleteyour code not working on google crome .Please help me
ReplyDeleteHai sir,
ReplyDeleteCan u please help me in recovering deleted Firefox web browser history?
Can i get history of google chrome's incognito mode browser by c#.
ReplyDeletecan you provide a Web application code for fetching history of web browser please
ReplyDeleteis it possible to get records which we deleted records with cookies
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi....without using Hardcode Path :- (+ @"\Google\Chrome\User Data\Default\History") lines; can we directly take the path from code itself using c#.net
ReplyDelete