我想写一个简单的程序,显示我在一段时间内的网络活动(我访问过哪个网站,多少次等等).我大多使用谷歌Chrome浏览器.我发现Chrome会在这个位置存储浏览器历史记录(如果我错了请纠正我)
C:\Documents and Settings\\Local Settings\Application Data\Google\Chrome\User Data\Default
如何打开历史文件?它们没有任何文件扩展名.我无法使用记事本,SQLite浏览器打开.如何以编程方式访问此数据?我想知道它是哪种文件格式以及如何使用C#等编程语言来读取它.
它只是一个SQlite 3数据库,我能够成功打开它(当然你无法打开正在运行的浏览器的锁定数据库).
这是我创建的用于从Google Chrome浏览器中读取浏览数据的课程.我从这里得到的大部分代码,但我调整了它以增加对谷歌浏览器的支持.您可能还想从此处下载.Net的SQLite,并添加对System.Data.Sqlite的引用.
class GoogleChrome { public ListURLs = new List (); public IEnumerable GetHistory() { // Get Current Users App Data string documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); string[] tempstr = documentsFolder.Split('\\'); string tempstr1 = ""; documentsFolder += "\\Google\\Chrome\\User Data\\Default"; if (tempstr[tempstr.Length - 1] != "Local") { for (int i = 0; i < tempstr.Length - 1; i++) { tempstr1 += tempstr[i] + "\\"; } documentsFolder = tempstr1 + "Local\\Google\\Chrome\\User Data\\Default"; } // Check if directory exists if (Directory.Exists(documentsFolder)) { return ExtractUserHistory(documentsFolder); } return null; } IEnumerable ExtractUserHistory(string folder) { // Get User history info DataTable historyDT = ExtractFromTable("urls", folder); // Get visit Time/Data info DataTable visitsDT = ExtractFromTable("visits", folder); // Loop each history entry foreach (DataRow row in historyDT.Rows) { // 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('\'', ' '), "Google Chrome"); // Add entry to list URLs.Add(u); } // Clear URL History DeleteFromTable("urls", folder); DeleteFromTable("visits", folder); return URLs; } void DeleteFromTable(string table, string folder) { SQLiteConnection sql_con; SQLiteCommand sql_cmd; // FireFox database file string dbPath = folder + "\\History"; // 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 + "\\History"; // 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; } }
URL的类:
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; } public string getData() { return browser + " - " + title + " - " + url; } }
它对我来说就像一个魅力.希望能帮助到你
为了查看sqlite数据库(这是Chromium历史索引),我更喜欢sqlitebrowser.它是免费的,适用于Windows,Mac和Linux.这对我来说是最好的.