C# 获取Google Chrome的书签

2021-07-01 10:06

阅读:588

标签:environ   else   closed   pat   回车符   inf   default   下标   chrome   

  其实这个很简单,就是读取一个在用户目录里面的一个Bookmarks文件就好了。

  先建立几个实体类

  

技术分享图片技术分享图片
 1 public class GoogleChrome_bookMark_meta_info
 2     {
 3         public string last_visited_desktop { get; set; }
 4     }
 5 
 6  public class GoogleChrome_BookMark_children
 7     {
 8         public string date_added { get; set; }
 9         public string id { get; set; }
10         public GoogleChrome_bookMark_meta_info meta_info { get; set; }
11         public string name { get; set; }
12         public string type { get; set; }
13         public string url { get; set; }
14         public List children { get; set; }
15 
16     }
17 
18 public class GoogleChrome_BookMark_bookmark_bar_other_synced
19     {
20         public string date_added { get; set; }
21         public string date_modified { get; set; }
22         public string id { get; set; }
23         public string name { get; set; }
24         public string type { get; set; }
25         public List children { get; set; }
26     }
27 
28 public class GoogleChrome_BookMark_roots
29     {
30         public GoogleChrome_BookMark_bookmark_bar_other_synced bookmark_bar { get; set; }
31         public GoogleChrome_BookMark_bookmark_bar_other_synced other { get; set; }
32         public GoogleChrome_BookMark_bookmark_bar_other_synced synced { get; set; }
33     }
34 
35  public class GoogleChrome_BookMarkAllModel
36     {
37         public string checksum { get; set; }
38         public GoogleChrome_BookMark_roots roots { get; set;}
39         public string version { get; set; }
40     }
View Code

 

  具体实现

技术分享图片技术分享图片
 1 static void Main(string[] args)
 2         {
 3             ///建几个Bookmarks 的实体类(Model)用来承载数据
 4             //获取由指定枚举标识的系统特殊文件夹的路径 ,, 其实就是读取用户目录下的Google Chrome 的Bookmarks 文件。
 5             string ChromeDatePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+@"\Google\Chrome\User Data\Default";
 6             string ChromeBookMarksPath = ChromeDatePath + @"\Bookmarks";
 7             if (File.Exists(ChromeBookMarksPath))
 8             {
 9                 Console.WriteLine("Chrome浏览器书签文件存在");
10             }
11             else
12                 Console.WriteLine("未找到Chrome浏览器书签");
13             Console.WriteLine(ChromeBookMarksPath);
14             StreamReader sr = new StreamReader(ChromeBookMarksPath);
15             string str = "";
16             while (!sr.EndOfStream)
17             {
18                 str += sr.ReadLine();
19             }
20             sr.Close();
21             string str2 = System.Text.RegularExpressions.Regex.Replace(str, "\\s*|\t|\r|\n", "");  //去空格、回车符
22             Console.WriteLine(str2);
23             //转为JSON格式
24             var obj = JsonConvert.DeserializeObject(str2);
25             if (obj.roots.bookmark_bar != null)
26                 ShowChildren(0, obj.roots.bookmark_bar.children);
27             Console.ReadKey();
28           
29         }
30 
31         /// 
32         /// 输出书签节点
33         /// 
34         /// 其实是没有用的,为了好看
35         /// 书签集合
36         public static void ShowChildren(int index, List children)
37         {
38             foreach (var l in children)
39             {
40                 Console.WriteLine("");
41                 Console.WriteLine(GetTreeStr(index) + "书签ID:" + l.id);
42                 Console.WriteLine(GetTreeStr(index) + "书签名称:" + l.name);
43                 Console.WriteLine(GetTreeStr(index) + "书签类型:" + l.type);
44                 if (l.type == "folder")
45                 {
46                     Console.WriteLine(GetTreeStr(index) + l.name + "是文件夹,文件夹下标签数量为:" + l.children.Where(w => w.type == "url").Count()
47                         + "文件夹数量为:" + l.children.Where(w => w.type == "folder").Count());
48                     Console.WriteLine(GetTreeStr(index) + l.name + "目录下内容");
49                 }
50                 else
51                     Console.WriteLine(GetTreeStr(index) + "书签url:" + l.url);
52             }
53         }
54         
55         /// 
56         /// 为了好看
57         /// 
58         /// 随便什么int数
59         /// 
60         public static string GetTreeStr(int index)
61         {
62             string str = "";
63             for (int i = 0; i )
64             {
65                 str += "...";
66             }
67             str += "L";
68             return str;
69         }
70     
View Code

 

C# 获取Google Chrome的书签

标签:environ   else   closed   pat   回车符   inf   default   下标   chrome   

原文地址:https://www.cnblogs.com/freedom-ly/p/9952446.html


评论


亲,登录后才可以留言!