Read Firefox bookmarks using C#

Judah Gabriel Himango picture Judah Gabriel Himango · Jun 3, 2009 · Viewed 7.8k times · Source

Using C#, I need to get all Firefox bookmarks for importing them into our database. How can I do this?

I'm aware of the SO question, Read FF 3 bookmarks in Java, but the answers there all seem to revolve around Java database drivers, and I'm not sure that some of those answers aren't Java-specific.

My primary question is, "How can I read Firefox bookmarks in C#?"

Secondary questions: I see \%user profile%\application data\mozilla\firefox\profiles\bookmarkbackups\bookmarks-[date].json files -- can I just parse that? If so, are there any existing parsers for that?

Rhetorical lamenting question: Why can't this be as easy as IE, where I just read the .url files in \%user profile%\favorites? Bah.

Answer

Nifle picture Nifle · Jun 3, 2009

Use the SQLite driver for .Net and access the file places.sqlite it can be found at
Application Data/Mozilla/Firefox/Profiles/$this_varies/places.sqlite
on my computer. It should not be hard for you to locate on your target computers.


Edit 1:
Here is a snip of code that prints out urls from the database:

using System.Data.SQLite; // downloaded from http://sourceforge.net/projects/adodotnetsqlite

namespace sqlite_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var path_to_db = @"C:\places.sqlite"; // copied here to avoid long path
            SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_db + ";Version=3;New=True;Compress=True;");

            SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();

            sqlite_connection.Open();

            sqlite_command.CommandText = "select * from moz_places";

            SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();

            while (sqlite_datareader.Read())
            {
                // Prints out the url field from the table:
                System.Console.WriteLine(sqlite_datareader["url"]);
            }
        }
    }
}

Edit 2:
As tip. I really must recommend the SQLite Manager plugin for firefox. It's very useful for working with sqlite databases.