Read Chrome Bookmark File

Posted by djopie on Wed, 24 Jun 2020 18:26:33 +0200

Use C#to read the Chrome browser's local bookmark file, the current file is Bookmarks under the User Folder \AppData\Local\Google\Chrome\User Data\Default\

Open this file and we find a json text file

{
   "checksum": "e723a57c9d9cbcae27ce0e1f8a5b7e71",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13157449994873867",
            "id": "5",
            "meta_info": {
               "last_visited_desktop": "13157449994874078"
            },
            "name": "Microsoft Bing search - Domestic version",
            "type": "url",
            "url": "https://cn.bing.com/"
         } ],
         "date_added": "13157449956838246",
         "date_modified": "13157449994873867",
         "id": "1",
         "name": "Bookmark Bar",
         "type": "folder"
      },
      "other": {
         "children": [  ],
         "date_added": "13157449956838251",
         "date_modified": "0",
         "id": "2",
         "name": "Other Bookmarks",
         "type": "folder"
      },
      "synced": {
         "children": [  ],
         "date_added": "13157449956838253",
         "date_modified": "0",
         "id": "3",
         "name": "Mobile Device Bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

Is json string converted to object for easy read operation Here, analyze the json structure to create the corresponding object

 1 namespace DataOperation.Model
 2 {
 3     /// <summary>
 4     /// Chorme Bookmark save file structure
 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 }

Using.NET provided Newtonsoft.Json.dll To do the conversion, two methods are pre-encapsulated here

 1         /// <summary>
 2         /// json serialize
 3         /// </summary>
 4         /// <typeparam name="T">data type</typeparam>
 5         /// <param name="data">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         /// Deserialize
24         /// </summary>
25         /// <typeparam name="T">data type</typeparam>
26         /// <param name="jsonstr">data</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);//Deserialize
35             }
36             catch (Exception e)
37             {
38 
39             }
40             return obj;
41         }

All that remains is to read the Bookmarks text file and convert it

The following code

 1         /// <summary>
 2         /// read file
 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         /// Obtain Chrome Browser Bookmark Object
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         }

Topics: C# JSON Google Mobile