7

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.

Community
  • 1
  • 1
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • it's not as easy as IE because the bookmarks in Firefox are more complicated than IEa nd because Firefox must be cross-platform. More complex requirements => more complex code. – Mr. Shiny and New 安宇 Jun 03 '09 at 16:16
  • 3
    IE stores them in plain text format as regular files. How is that not cross platform? – Judah Gabriel Himango Jun 03 '09 at 16:27
  • IE way of doing things is not as better as FF's for at least one reason. A file name cannot contain some special characters like a forward slash (/). Not to mention that FF also supports tagging on bookmarks. – Ionuț G. Stan Jun 03 '09 at 16:38
  • 2
    True, but that's not a problem that couldn't be resolved with regular files. Just put a [Title] in plain text format or in XML. Sigh. Going with a database just seems like overkill. Oh well, maybe I'm just being pessimistic. :-) – Judah Gabriel Himango Jun 03 '09 at 16:46

5 Answers5

7

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.

Nifle
  • 11,745
  • 10
  • 75
  • 100
  • Thanks, I've marked this as the correct answer. It looks like the backup files are stored in .json format, but only for FF3 and later. For folks reading this, bottom line is this: -FF3 uses SQLLite to store bookmarks. It uses .json files to store backups of the bookmarks. -FF2 usees the bookmarks.html file to store bookmarks. – Judah Gabriel Himango Jun 03 '09 at 16:55
  • 1
    Worth mentioning that actual official website for the SQLite lib is http://sqlite.phxsoftware.com - amazing product, so polished and in the public domain too! – Nathan Ridley Jul 02 '09 at 18:08
  • 1
    FYI, the SQLite ADO.NET provider you mentioned is not being maintained anymore. I recommend this one instead : http://sqlite.phxsoftware.com/. It is very complete and even supports the Entity Framework. – Thomas Levesque Jul 02 '09 at 18:10
  • @Nathan : you beat me by a minute ;) – Thomas Levesque Jul 02 '09 at 18:12
  • You can install System.Data.SQLite provider using Visual Studio Package Manager: PM> Install-Package System.Data.SQLite – Gordon Bell Jul 14 '17 at 20:22
2

Surely it works the same way as suggested in the Java question, just get the SQLite .NET provider and use that to access the FF database file.

Tom van Enckevort
  • 4,170
  • 1
  • 25
  • 40
1

I had to rework this slightly for my project http://www.codertakeout.com. Hope this revision helps clarify a few things thanks to some suggestions from around the web.

using System.Data.SQLite;  // need to install sqlite .net driver

String path_to_db = @"C:\Documents and Settings\Jeff\Application Data\Mozilla\Firefox\Profiles\yhwx4xco.default\places.sqlite";
String path_to_temp = System.IO.Path.GetTempFileName();

System.IO.File.Copy(path_to_db, path_to_temp, true);
SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_temp + ";Version=3;Compress=True;Read Only=True;");

SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();

sqlite_connection.Open();

sqlite_command.CommandText = "SELECT moz_bookmarks.title,moz_places.url FROM moz_bookmarks LEFT JOIN moz_places WHERE moz_bookmarks.fk = moz_places.id AND moz_bookmarks.title != 'null' AND moz_places.url LIKE '%http%';";

SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();

while (sqlite_datareader.Read())
    {
        System.Console.WriteLine(sqlite_datareader[1]);
    }
sqlite_connection.Close();
System.IO.File.Delete(path_to_temp);
jeff
  • 46
  • 1
1

Visit http://myexps.blogspot.com for the implementation in java.

import java.sql.*;

public class helloWorld {
  public static void main(String[] args) throws Exception {
      Class.forName("org.sqlite.JDBC");
      Connection conn = DriverManager.getConnection("jdbc:sqlite:/home/deepak/.mozilla/firefox/yvf7p20d.default/places.sqlite//");
  if(conn==null)
  {
   System.out.println("ERROR");
  }
  System.out.println(conn.toString());

  Statement stat = conn.createStatement();

  ResultSet rs = stat.executeQuery("select * from moz_bookmarks;");
  while (rs.next()) {
      System.out.println("id = " + rs.getString("id"));
      System.out.println("keyword = " + rs.getString("keyword_id"));
      System.out.println("title = " + rs.getString("title"));
  }
  rs.close();
  conn.close();
  }
}

This will be the java implementation

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
Deepak
  • 11
  • 1
1

There's a SQLite driver for .Net. Once you get that working I'd imagine the solution would be the same in both .Net and Java.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147