Logo

Logo

C# 获取Google Chrome的书签

Pastore Antonio
Pastore Antonio 2024年06月25日
375 阅读 0 评论 约 4722 字 阅读约 10 分钟

  其实这个很简单,就是读取一个在用户目录里面的一个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

 

查看完整代码

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
部分文章可能存在转载,如果涉及到侵权,请联系删除文章。

探索AIGC相关的精彩内容,共 14 篇文章

MCP | 一文详解什么是 MCP以及 MCP 可以做什么

一、什么是 MCP MCP(Model Context Protocol)是一个专为大型语言模型(L ... C# 获取Google Chrome的书签

2026-02-14 · Shen, Luke
你的工作流程,值得一个“全自动数字分身”:录制、截图、成文,一气呵成

一、一句话认识 TestFlow Recorder 在数字化工作环境中,如何准确记录操作步骤并生成清 ... C# 获取Google Chrome的书签

2026-02-14 · Xzavier Aaron
Flowise 前端框架配置指南

用户需求 问题:有没有适合配置 Flowise 的前端框架? 目标:寻找类似 Open WebUI ... C# 获取Google Chrome的书签

2026-02-14 · Xzavier Aaron
高效AI故障诊断实践:提升企业运维能力

规划阶段 在现代企业的故障诊断过程中,规划阶段是确保诊断工作顺利开展的关键环节。通过系统化的规划,不 ... C# 获取Google Chrome的书签

2026-02-14 · Shen, Luke