码迷,mamicode.com
首页 > Windows程序 > 详细

C# 获取Google Chrome的书签

时间:2018-11-13 16:17:33      阅读:285      评论:0      收藏:0      [点我收藏+]

标签: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<GoogleChrome_BookMark_children> 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<GoogleChrome_BookMark_children> 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<GoogleChrome_BookMarkAllModel>(str2);
25             if (obj.roots.bookmark_bar != null)
26                 ShowChildren(0, obj.roots.bookmark_bar.children);
27             Console.ReadKey();
28           
29         }
30 
31         /// <summary>
32         /// 输出书签节点
33         /// </summary>
34         /// <param name="index">其实是没有用的,为了好看</param>
35         /// <param name="children">书签集合</param>
36         public static void ShowChildren(int index, List<GoogleChrome_BookMark_children> 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         /// <summary>
56         /// 为了好看
57         /// </summary>
58         /// <param name="index">随便什么int数</param>
59         /// <returns></returns>
60         public static string GetTreeStr(int index)
61         {
62             string str = "";
63             for (int i = 0; i < index; 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!