7

使用 C#,我需要获取所有 Firefox 书签,以便将它们导入我们的数据库。我怎样才能做到这一点?

我知道 SO 问题Read FF 3 bookmarks in Java,但那里的答案似乎都围绕 Java 数据库驱动程序,我不确定其中一些答案不是 Java 特定的。

我的主要问题是,“如何在 C# 中阅读 Firefox 书签?”

次要问题:我看到 \%user profile%\application data\mozilla\firefox\profiles\bookmarkbackups\bookmarks-[date].json 文件——我可以解析吗?如果是这样,是否有任何现有的解析器?

修辞感叹的问题:为什么不能像 IE 那样简单,我只是在 \%user profile%\favorites 中读取 .url 文件?呸。

4

5 回答 5

7

使用 .Net 的 SQLite 驱动程序并访问可以在我的计算机上找到的文件places.sqlite 。
Application Data/Mozilla/Firefox/Profiles/$this_varies/places.sqlite
在目标计算机上定位应该不难。


编辑 1:
这是从数据库中打印出 url 的代码片段:

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"]);
            }
        }
    }
}

编辑2:
作为提示。我真的必须为 firefox 推荐 SQLite Manager 插件。它对于使用 sqlite 数据库非常有用。

于 2009-06-03T16:32:27.810 回答
2

当然,它的工作方式与 Java 问题中建议的方式相同,只需获取SQLite .NET 提供程序并使用它来访问 FF 数据库文件。

于 2009-06-03T15:59:02.650 回答
1

.Net 有一个 SQLite 驱动程序。一旦你开始工作,我想解决方案在 .Net 和 Java 中都是一样的。

于 2009-06-03T15:57:01.107 回答
1

我不得不为我的项目http://www.codertakeout.com稍微修改一下。希望这次修订有助于澄清一些事情,这要归功于网络上的一些建议。

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);
于 2009-07-02T18:03:46.223 回答
1

访问http://myexps.blogspot.com以了解 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();
  }
}

这将是java实现

于 2009-11-27T16:51:28.770 回答