- Internet Explorer
- public class InternetExplorer
- {
- // List of URL objects
- public List<URL> URLs { get; set; }
- public IEnumerable<URL> GetHistory()
- {
- // Initiate main object
- UrlHistoryWrapperClass urlhistory = new UrlHistoryWrapperClass();
- // Enumerate URLs in History
- UrlHistoryWrapperClass.STATURLEnumerator enumerator =
- urlhistory.GetEnumerator();
- // Iterate through the enumeration
- while (enumerator.MoveNext())
- {
- // Obtain URL and Title
- string url = enumerator.Current.URL.Replace('\'', ' ');
- // In the title, eliminate single quotes to avoid confusion
- string title = string.IsNullOrEmpty(enumerator.Current.Title)
- ? enumerator.Current.Title.Replace('\'', ' ') : "";
- // Create new entry
- URL U = new URL(url, title, "Internet Explorer");
- // Add entry to list
- URLs.Add(U);
- }
- // Optional
- enumerator.Reset();
- // Clear URL History
- urlhistory.ClearHistory();
- return URLs;
- }
- }
- Mozilla Firefox
- public class Firefox
- {
- public List<URL> URLs { get; set; }
- public IEnumerable<URL> GetHistory()
- {
- // Get Current Users App Data
- string documentsFolder = Environment.GetFolderPath
- (Environment.SpecialFolder.ApplicationData);
- // Move to Firefox Data
- documentsFolder += "\\Mozilla\\Firefox\\Profiles\\";
- // Check if directory exists
- if (Directory.Exists(documentsFolder))
- {
- // Loop each Firefox Profile
- foreach (string folder in Directory.GetDirectories
- (documentsFolder))
- {
- // Fetch Profile History
- return ExtractUserHistory(folder);
- }
- }
- return null;
- }
- IEnumerable<URL> ExtractUserHistory(string folder)
- {
- // Get User history info
- DataTable historyDT = ExtractFromTable("moz_places", folder);
- // Get visit Time/Data info
- DataTable visitsDT = ExtractFromTable("moz_historyvisits",
- folder);
- // Loop each history entry
- foreach (DataRow row in historyDT.Rows)
- {
- // Select entry Date from visits
- var entryDate = (from dates in visitsDT.AsEnumerable()
- where dates["place_id"].ToString() == row["id"].ToString()
- select dates).LastOrDefault();
- // If history entry has date
- if (entryDate != null)
- {
- // Obtain URL and Title strings
- string url = row["Url"].ToString();
- string title = row["title"].ToString();
-
- // Create new Entry
- URL u = new URL(url.Replace('\'', ' '),
- title.Replace('\'', ' '),
- "Mozilla Firefox");
- // Add entry to list
- URLs.Add(u);
- }
- }
- // Clear URL History
- DeleteFromTable("moz_places", folder);
- DeleteFromTable("moz_historyvisits", folder);
- return URLs;
- }
- void DeleteFromTable(string table, string folder)
- {
- SQLiteConnection sql_con;
- SQLiteCommand sql_cmd;
- // FireFox database file
- string dbPath = folder + "\\places.sqlite";
- // If file exists
- if (File.Exists(dbPath))
- {
- // Data connection
- sql_con = new SQLiteConnection("Data Source=" + dbPath +
- ";Version=3;New=False;Compress=True;");
- // Open the Conn
- sql_con.Open();
- // Delete Query
- string CommandText = "delete from " + table;
- // Create command
- sql_cmd = new SQLiteCommand(CommandText, sql_con);
- sql_cmd.ExecuteNonQuery();
- // Clean up
- sql_con.Close();
- }
- }
- DataTable ExtractFromTable(string table, string folder)
- {
- SQLiteConnection sql_con;
- SQLiteCommand sql_cmd;
- SQLiteDataAdapter DB;
- DataTable DT = new DataTable();
- // FireFox database file
- string dbPath = folder + "\\places.sqlite";
- // If file exists
- if (File.Exists(dbPath))
- {
- // Data connection
- sql_con = new SQLiteConnection("Data Source=" + dbPath +
- ";Version=3;New=False;Compress=True;");
- // Open the Connection
- sql_con.Open();
- sql_cmd = sql_con.CreateCommand();
- // Select Query
- string CommandText = "select * from " + table;
- // Populate Data Table
- DB = new SQLiteDataAdapter(CommandText, sql_con);
- DB.Fill(DT);
- // Clean up
- sql_con.Close();
- }
- return DT;
- }
- }
This was the easiest browser whose URL history can be obtained and deleted at will. My research started off with a Registry discovery - URLs typed in the address box of IE are captured in the Registry key HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs. But that was way short of the purpose. Then, I came across the COM interface IUrlHistory on MSDN. But it was COM and writing a C# class wrapper or something was something I was not proficient in at all. So, I reluctantly began searching for ready code. And voila!
An article on CodeProject was all it took to serve my purpose. It provides a UrlHistoryLibrary.dll file which provides type-safe classes to access and manipulate IE's browsing history. Details on how to use the classes are given in the article itself. However, this is how I used it.
Notes:
The code is fairly self-explanatory except for a few clarifications. The URL class used above is a simple class which has three string fields - URL, Title and BrowserName - and URLs is a List
<
URL>
. Those little string.Replace functions are to eliminate single quotes in the URLs so that there are no problems inserting them into an SQL database.The same thing for Mozilla Firefox was way tougher. First of all, it was difficult to locate where Firefox stores the browsing history. The answer came in the form of an SQLite database which is stored in the Application Data of the Windows user. So, I had a SQLite database at hand. First, I tried FileStream to the .sqlite file. I had opened the file in Notepad++ and seen that URLs were interspersed with quirky characters, so using the FileStream, I could manage to obtain the URLs, but obtaining the Titles was impossible. Moreover, I was never sure that a URL I come across was ever visited or was it a bookmark or what. So, again some code-googling later, I came across the ADO.NET Data Provider for SQLite.The download page is here.
What is wonderful about this Provider is that not only it provided a .NET-native namespace (System.Data.SQLite), but also it provides full Visual Studio 2008 design-time support. So, without wasting more time, let me provide you the code.
So, while some of this code was taken from the mozillazine forums, some was written on my own and the combination worked perfectly well for me in my final year project. Hope it works for you, too!
the code is not working on VS 2010
ReplyDeleteI think you focused only on the code instead of reading the blog post entirely.
ReplyDeleteFor the IE code to work, you need to add IUrlHistory.dll to the project references. The link for the dll is provided in the blog post.
For the Firefox code to work, you need ADO.NET Data Provider for SQLite, again whose dll link has been provided in the blog post.
Hope this helps.
thanks for your reply!
ReplyDeletei have read the blog very carefully!!!
the IE code is working fine!!!
i have installed the SQLite as well but i m getting error for list URL "public List URLs { get; set; }"
further you have any idea how to do the same with Opera and Chrome browsers?
ReplyDeleteI have not defined the the URL class which I have used, so I think that's a mistake on my part. You can go ahead and make a class that looks like this.
ReplyDeletepublic 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;
}
}
I think this should do it.
Hi, I have had a lot of trouble getting the SQLite running in C#.... can you possibly help?
ReplyDeleteCode looks great otherwise, setting up visual studio is frustrating sometimes though.
Thanks - falazar@yahoo.com
Hi, I was wondering if you could help, am attempting the FF version, and having troubel with SQLite and Visual Studio... thanks
ReplyDeletefalazar@yahoo.com -
James
Hi , I am an error as type or namespace is not found.....
ReplyDeletecan u tell me....
Can you be more specific? If you can send me a screenshot or something or just point out the line where you are getting the error, I could help you.
ReplyDeleteHowever, these steps may help.
In case of Firefox:
In the solution you are working on, add a reference to the SQLite dlls. If you have installed the ADO.NET provider for SQLite correctly, you will get the dlls at the following locations:
C:\Program Files\SQLite.NET\bin\System.Data.SQLite.dll
C:\Program Files\SQLite.NET\bin\System.Data.SQLite.Linq.dll
Then add a "using System.Data.SQLite;" statement in the usings of your code file and you should be good to go.
In case of Internet Explorer:
In the article, I have pointed to a CodeProject article which contains a solution for IUrlHistory. You can get it here. (The catch is that you have to be a CodeProject member, but it's free).
So, open the solution in Visual Studio, build it successfully once and then navigate to its bin directory. There, you will find a UrlHistoryLibrary.dll file. Copy it to a suitable location.
In the solution you are working on, add a reference to this dll and add a "using UrlHistoryLibrary;" statement in the usings of your code file.
Hope this helps.
Thanks Joel...
ReplyDeleteIt's Working.......
Thank you very much. That's what I'm trying to do.
ReplyDeleteHii,
ReplyDeleteMany thanks for sharing nice code sample. But,
do you think the folder: "\\Mozilla\\Firefox\\Profiles\\" exists in local hard drive??
I have installed mozila 4.0 which does not creates such folder path, now will your code work okay?
Hi,
ReplyDeleteCurrently we get an issue database is locked but when we close the firefox it works fine for me.
Please suggest me that it works fine in the case of open firefox also??
@Karthik: Make sure you have added a project reference to System.Data.dll. If yes, you must have System.Data namespace added in your code file. Sorry for the late reply.
ReplyDelete@Pankaj: Correct. It does not work when firefox is running. Sorry for the late reply.
ReplyDelete@Navin C: This code is guaranteed to work for Firefox 3 series only. For Firefox 4, get started here:
ReplyDeletehttp://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/
Sorry for the late reply.
Error I got is :
ReplyDeleteThe name directory does not exist in the current context.
I tried in FireFox 11 . Please provide a solution .
binary have you already sorted this issue out?
DeleteTry adding one of this directives at the top of the class:
Deleteusing System.IO;
Thanks Joel for the codes above. Very helpful. I have also added a new class to add support for Google Chrome and now it works like a charm for all 3 browsers. Cheers
ReplyDeletehi, i can't have any results using this code with internet explorer,( i need this in my final year project), i am new with C#, i wonder if you can help me with google chrom classes, hope that's possible
Deletei am having an exception at this line URLs.Add(U); VC2010 suggests to add ( new) somewhere... i really can't understand this error!!
Hi, people! For Google Chrome, I have a new post for you: http://hardcodedblog.blogspot.in/2012/01/get-web-browser-history-in-c-google.html
DeleteMicha, I hope you have resolved the VC2010 error by now. :)
Ok, I also have a NullReferenceException for
DeleteURLs.Add(U), any ideas?
Ok, for anyone who cannot get the IE code to work, see my blog at
Deletethestrangertech.blogspot.com
for a solution.
hi, can anyone tell me how to parse History.dat file in opera location.
ReplyDeleteI 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." when I am running code for firefox.. plz help
ReplyDeleteswati
has any one worked on code for latest version of firefox.
ReplyDeleteswati
I try to use y this code in web part and deploy it ti sharepoint site
ReplyDeletebut it not work
i have SecuritException
can U help me please?
thanks
Hi Mostafa, this code gets the browser history of the machine on which it is executing. As such, it is supposed to work only in Desktop applications. Web applications do not have permission to access the local resources that this code accesses.
DeleteEven then, can you post the complete exception that you are getting on the Sharepoint site?
Hi Joel Mathias,
ReplyDeletesorry i didn't see your reply before now.
when i try to use this code in web part
the exception was
SecurityException was unhandled by user code
"System.security.permissions.securitypermission"
this appear in this section
public UrlHistoryWrapperClass()
{
urlHistory = new UrlHistoryClass(); ///////Here
obj = (IUrlHistoryStg2) urlHistory;
}
Thanks.
add the namespace system.data i hope it will work fine . Thanks in advance.
ReplyDeleteHi you have nice code hre, i was try to convert the code to Vb.Net, everything is good but i have error on Private Function ExtractUserHistory (The line: Dim entryDate = (From dates In visitsDT.AsEnumerable() Where dates("place_id").ToString() = row("id").ToString() dates).LastOrDefault()
ReplyDeleteIt says ")" expected
Can you help me with that?! Thank you
hi.. i am getting error in Directory and File. Can any one help me out.
ReplyDeletethank you :)
ReplyDeleteplease can you show the same in vb.net.? I need the ful working code
ReplyDeleteHello I am using this code but when i run it threw an exception.
ReplyDeletecan you please help me to solve this issue. ecption threw in URLs.Add(U);
HI All, I'm not able to download "UrlHistoryLibrary.dll" file from anywhere. I've spent lots of time to download this dll. Please provide me any link so that I can download this.
ReplyDeleteThanks in advance
It's in the Code project page referenced in the article.
DeleteHi All, I've to the same as described above using visual studio 2010. But, not able to get IE browsing history. Kindly suggest me about any solution.
ReplyDeleteThanks in advance...
Hello, I need to capture web history for Google Chrome and Firefox. can any one please help me with the tool? as i am not a experience developer, can not write high level code. you can reach me on andy010679@gmail.com. Thank you.
ReplyDeleteThis was such an uplifting read! I’m grateful for the positivity and knowledge you share. Can’t wait for more!
ReplyDelete