使用C#讀取Chrome瀏覽器的本地書簽文件,當前文件在C盤下用戶文件夾\AppData\Local\Google\Chrome\User Data\Default\下的Bookmarks 打開這個文件我們發現就是一個json文本文件 為了方便讀取操作還是將json字元串轉換為對象這裡分析json結 ...
使用C#讀取Chrome瀏覽器的本地書簽文件,當前文件在C盤下用戶文件夾\AppData\Local\Google\Chrome\User Data\Default\下的Bookmarks
打開這個文件我們發現就是一個json文本文件
{ "checksum": "e723a57c9d9cbcae27ce0e1f8a5b7e71", "roots": { "bookmark_bar": { "children": [ { "date_added": "13157449994873867", "id": "5", "meta_info": { "last_visited_desktop": "13157449994874078" }, "name": "微軟 Bing 搜索 - 國內版", "type": "url", "url": "https://cn.bing.com/" } ], "date_added": "13157449956838246", "date_modified": "13157449994873867", "id": "1", "name": "書簽欄", "type": "folder" }, "other": { "children": [ ], "date_added": "13157449956838251", "date_modified": "0", "id": "2", "name": "其他書簽", "type": "folder" }, "synced": { "children": [ ], "date_added": "13157449956838253", "date_modified": "0", "id": "3", "name": "移動設備書簽", "type": "folder" } }, "version": 1 }
為了方便讀取操作還是將json字元串轉換為對象這裡分析json結構創建對應的對象
1 namespace DataOperation.Model 2 { 3 /// <summary> 4 /// Chorme書簽保存文件結構 5 /// </summary> 6 public class ChromeBookmarks 7 { 8 public string checksum { get; set; } 9 public bookmark roots { get; set; } 10 //public string sync_transaction_version { get; set; } 11 public string version { get; set; } 12 //public string synced { get; set; } 13 } 14 15 public class bookmark 16 { 17 public datameta bookmark_bar { get; set; } 18 public datameta other { get; set; } 19 } 20 21 public class datameta 22 { 23 public List<datameta> children { get; set; } 24 public string date_added { get; set; } 25 public string date_modified { get; set; } 26 public string id { get; set; } 27 public meta_info meta_info { get; set; } 28 public string name { get; set; } 29 public string sync_transaction_version { get; set; } 30 public string type { get; set; } 31 public string url { get; set; } 32 } 33 34 public class meta_info 35 { 36 public string last_visited_desktop { get; set; } 37 } 38 }View Code
利用.NET提供的Newtonsoft.Json.dll來進行轉換這裡預先封裝兩個方法
1 /// <summary> 2 /// json序列化 3 /// </summary> 4 /// <typeparam name="T">數據類型</typeparam> 5 /// <param name="data">數據</param> 6 /// <returns></returns> 7 public static string ListToJson<T>(T data) 8 { 9 string str = string.Empty; 10 try 11 { 12 if (null != data) 13 str = JsonConvert.SerializeObject(data); 14 } 15 catch (Exception e) 16 { 17 18 } 19 return str; 20 } 21 22 /// <summary> 23 /// 反序列化 24 /// </summary> 25 /// <typeparam name="T">數據類型</typeparam> 26 /// <param name="jsonstr">數據</param> 27 /// <returns></returns> 28 public static Object JsonToList<T>(string jsonstr) 29 { 30 Object obj = null; 31 try 32 { 33 if (null != jsonstr) 34 obj = JsonConvert.DeserializeObject<T>(jsonstr);//反序列化 35 } 36 catch (Exception e) 37 { 38 39 } 40 return obj; 41 }View Code
剩下的就是將Bookmarks文本文件讀取後進行轉換了
下麵部分代碼
1 /// <summary> 2 /// 讀取文件 3 /// </summary> 4 /// <param name="filePath"></param> 5 /// <returns></returns> 6 public static string FileRead(string filePath) 7 { 8 string rel = File.ReadAllText(filePath); 9 return rel; 10 } 11 12 /// <summary> 13 /// 獲取Chrome瀏覽器書簽對象 14 /// </summary> 15 /// <param name="filePath"></param> 16 /// <returns></returns> 17 public ChromeBookmarks GetChromeBookmarksData(string filePath) 18 { 19 string str = FileRead(filePath); 20 object chromeBookmarks = JsonToList<ChromeBookmarks>(str); 21 if (chromeBookmarks != null) 22 { 23 return (ChromeBookmarks)chromeBookmarks; 24 } 25 return null; 26 }View Code