其实这个很简单,就是读取一个在用户目录里面的一个Bookmarks文件就好了。
先建立几个实体类
public class GoogleChrome_bookMark_meta_info
{
public string last_visited_desktop { get; set; }
} public class GoogleChrome_BookMark_children
{
public string date_added { get; set; }
public string id { get; set; }
public GoogleChrome_bookMark_meta_info meta_info { get; set; }
public string name { get; set; }
public string type { get; set; }
public string url { get; set; }
public List<GoogleChrome_BookMark_children> children { get; set; } } public class GoogleChrome_BookMark_bookmark_bar_other_synced
{
public string date_added { get; set; }
public string date_modified { get; set; }
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
public List<GoogleChrome_BookMark_children> children { get; set; }
} public class GoogleChrome_BookMark_roots
{
public GoogleChrome_BookMark_bookmark_bar_other_synced bookmark_bar { get; set; }
public GoogleChrome_BookMark_bookmark_bar_other_synced other { get; set; }
public GoogleChrome_BookMark_bookmark_bar_other_synced synced { get; set; }
} public class GoogleChrome_BookMarkAllModel
{
public string checksum { get; set; }
public GoogleChrome_BookMark_roots roots { get; set;}
public string version { get; set; }
}
具体实现
static void Main(string[] args)
{
///建几个Bookmarks 的实体类(Model)用来承载数据
//获取由指定枚举标识的系统特殊文件夹的路径 ,, 其实就是读取用户目录下的Google Chrome 的Bookmarks 文件。
string ChromeDatePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+@"\Google\Chrome\User Data\Default";
string ChromeBookMarksPath = ChromeDatePath + @"\Bookmarks";
if (File.Exists(ChromeBookMarksPath))
{
Console.WriteLine("Chrome浏览器书签文件存在");
}
else
Console.WriteLine("未找到Chrome浏览器书签");
Console.WriteLine(ChromeBookMarksPath);
StreamReader sr = new StreamReader(ChromeBookMarksPath);
string str = "";
while (!sr.EndOfStream)
{
str += sr.ReadLine();
}
sr.Close();
string str2 = System.Text.RegularExpressions.Regex.Replace(str, "\\s*|\t|\r|\n", ""); //去空格、回车符
Console.WriteLine(str2);
//转为JSON格式
var obj = JsonConvert.DeserializeObject<GoogleChrome_BookMarkAllModel>(str2);
if (obj.roots.bookmark_bar != null)
ShowChildren(, obj.roots.bookmark_bar.children);
Console.ReadKey(); } /// <summary>
/// 输出书签节点
/// </summary>
/// <param name="index">其实是没有用的,为了好看</param>
/// <param name="children">书签集合</param>
public static void ShowChildren(int index, List<GoogleChrome_BookMark_children> children)
{
foreach (var l in children)
{
Console.WriteLine("");
Console.WriteLine(GetTreeStr(index) + "书签ID:" + l.id);
Console.WriteLine(GetTreeStr(index) + "书签名称:" + l.name);
Console.WriteLine(GetTreeStr(index) + "书签类型:" + l.type);
if (l.type == "folder")
{
Console.WriteLine(GetTreeStr(index) + l.name + "是文件夹,文件夹下标签数量为:" + l.children.Where(w => w.type == "url").Count()
+ "文件夹数量为:" + l.children.Where(w => w.type == "folder").Count());
Console.WriteLine(GetTreeStr(index) + l.name + "目录下内容");
}
else
Console.WriteLine(GetTreeStr(index) + "书签url:" + l.url);
}
} /// <summary>
/// 为了好看
/// </summary>
/// <param name="index">随便什么int数</param>
/// <returns></returns>
public static string GetTreeStr(int index)
{
string str = "";
for (int i = ; i < index; i++)
{
str += "...";
}
str += "L";
return str;
}