使用c#从浏览器本地存储检索数据

编程入门 行业动态 更新时间:2024-10-24 11:16:01
本文介绍了使用c#从浏览器本地存储检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以使用C#从chrome / firefox本地存储检索数据?

Is it possible to retrieve data from chrome/firefox local storage using C#?

推荐答案

免责声明:我的Windows 7 x64目前运行谷歌Chrome 13.0.782.220。这里提供的信息是我自己的研究结果,并不是任何官方的方式或API来检索这些信息。使用风险自负。此外,如果Chrome更改存储此信息的方式,此处提供的技术可能会在未来的任何版本中崩溃。

Disclaimer: I have tested this on my Windows 7 x64 running Google Chrome 13.0.782.220 at the moment. The information provided here is a result of my own research and is not any official way or API to retrieve this information. Use at your own risk. Also the technique presented here might break with any future release if Chrome changes the way to store this information.

Chrome使用SQLite来持久保存本地存储数据。您可以使用 System.Data.SQLite 托管驱动程序从.NET应用程序中读取它。如果你是在Windows 7上运行(不知道为别人,因为那是我有,可以测试),你将有以下文件夹:

So, Google Chrome uses SQLite to persist local storage data. You could use the System.Data.SQLite managed driver to read it from your .NET application. If you are running on Windows 7 (don't know for others as that's the one I have and can test), you will have the following folder:

c:\Users\SOMEUSERNAME\AppData\Local\Google\Chrome\User Data\Default\Local Storage\

此文件夹将包含多个具有 .localstorage 扩展名的文件。每个文件是为不同的网站。例如对于StackOverflow我有 http_stackoverflow_0.localstorage ,但当然这个命名是完全任意的,你不能依赖它。每个文件代表一个SQLite数据库。

This folder will contain multiple files with the .localstorage extension. Each file is for different site. For example for StackOverflow I have http_stackoverflow_0.localstorage but of course this naming is totally arbitrary and you cannot rely upon it. Each file represents a SQLite database.

我注意到这个数据库包含一个名为 ItemTable 调用键和 value 。

I have noticed that this database contains a table called ItemTable with 2 string columns called key and value.

值是一个简单的事情发送SQL查询:

So to read the values it's a simple matter of sending a SQL query:

class Program { static void Main() { using (var conn = new SQLiteConnection("Data Source=http_stackoverflow_0.localstorage;Version=3;")) using (var cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = "SELECT key, value FROM ItemTable"; using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine( "key: {0}, value: {1}", reader.GetString(reader.GetOrdinal("key")), reader.GetString(reader.GetOrdinal("value")) ); } } } } }

更多推荐

使用c#从浏览器本地存储检索数据

本文发布于:2023-10-11 22:34:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!