使用C#读取Chrome浏览器的本地书签文件,当前文件在C盘下用户文件夹AppDataLocalGoogleChromeUser DataDefault下的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 }
利用.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 }
剩下的就是将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 }