【发布时间】:2015-11-10 14:35:13
【问题描述】:
我需要阅读特定网站的一些 cookie,并且我在互联网上找到了可能对我有帮助的代码。这段代码使用了 SQLite 的一些特定方法,因此必须添加对一些 SQLite dll 的引用,但问题是,当我添加 SQlite.Interop.dll 时,会生成一个错误提示:
无法添加对“C:\Program Files\System.Data.SQLite\2012\bin\SQLite.Interop.dll”的引用。请确保该文件是可访问的,并且它是一个有效的程序集或 COM 组件。
所以,有人可以帮我解决这个麻烦。我已经在互联网上的几个网站上看到了与它相关的东西,但直到现在,我还没有成功。
这是我找到的代码,他需要 SQLite dll 文件引用,就像我上面说的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static public IEnumerable<Tuple<string, string>> ReadCookies(string hostName)
{
if (hostName == null) throw new ArgumentNullException(hostName);
var dbPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Google\Chrome\User Data\Default\Cookies";
if (!System.IO.File.Exists(dbPath)) throw new System.IO.FileNotFoundException("Cant find cookie store", dbPath);
var connectionString = "Data Source=" + dbPath + ";pooling=false";
using (var conn = new System.Data.SQLite.SQLiteConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
var prm = cmd.CreateParameter();
prm.ParameterName = hostName;
prm.Value = hostName;
cmd.Parameters.Add(prm);
cmd.CommandText = "SELECT name,encrypted_value FROM cookies WHERE host_key = " + hostName;
conn.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var encryptedData = (byte[])reader[1];
var decodedData = System.Security.Cryptography.ProtectedData.Unprotect(encryptedData, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);
var plainText = Encoding.ASCII.GetString(decodedData);
yield return Tuple.Create(reader.GetString(0), plainText);
}
}
conn.Close();
}
}
static void Main(string[] args)
{
var list = ReadCookies("facebook.com");
foreach (var item in list)
Console.WriteLine("{0} | {1}", item.Item1, item.Item2);
Console.WriteLine();
Console.ReadLine();
}
}
}
【问题讨论】:
-
请注意,不是每个人都能看到显示您的错误消息的图像(例如,我现在无法看到它)。因此,如果您可以包含错误消息的文本而不是链接到它作为图像,那么对每个人都会更好。不仅每个人都能看到错误,而且错误(以及未来的答案)也将被谷歌索引。
-
@PeterM,我编辑了我的问题。
-
如果你有兴趣,我已经添加了消息@PeterM
标签: c# sqlite dll-reference