使用C#读取Firefox书签

时间:2009-06-03 15:54:40

标签: c# firefox bookmarks favorites

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

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

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

次要问题:我看到\%用户个人资料%\应用数据\ mozilla \ firefox \ profiles \ bookmarkbackups \ bookmarks- [date] .json文件 - 我可以解析一下吗?如果是这样,是否有任何现有的解析器?

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

5 个答案:

答案 0 :(得分: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数据库非常有用。

答案 1 :(得分:2)

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

答案 2 :(得分:1)

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

答案 3 :(得分: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);

答案 4 :(得分: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实现