使用c#从浏览器本地存储检索数据 [英] Retrieve data from browser local storage using c#

查看:151
本文介绍了使用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.com_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.com_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.com_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#从浏览器本地存储检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆