【问题标题】:How do I save a cookie to a file in Metro?如何将 cookie 保存到 Metro 中的文件中?
【发布时间】:2012-07-15 00:55:16
【问题描述】:

基本上,我需要知道如何在 .net 4.5 中编写此代码。我在 MSDN 中找不到它。

private void Savecookie(string filename, CookieContainer rcookie)
{
    Stream stream = File.Open(filename, FileMode.Create);
    BinaryFormatter bFormatter = new BinaryFormatter();
    bFormatter.Serialize(stream, rcookie);
    stream.Close();
}

文件已被存储文件夹替换,我找不到 binaryformatter 的替换。我不知道如何序列化文件的数据。

【问题讨论】:

    标签: c# .net microsoft-metro .net-4.5


    【解决方案1】:

    您可以使用MemoryStream 将数据作为字节数组获取,然后您可以将其保存到 StorageFile。

    private byte[] SerializeCookies(CookieContainer rcookie) 
    { 
        MemoryStream stream = new MemoryStream();
        BinaryFormatter bFormatter = new BinaryFormatter(); 
        bFormatter.Serialize(stream, rcookie); 
        stream.Close(); 
        return stream.ToArray();
    } 
    

    【讨论】:

    • 谢谢,但问题是序列化。由于某种原因,metro runtime.serialization 库中不存在 BinaryFormatter,并且 xmlserializer 没有正确序列化 cookiecontainer 对象。
    • @user1526306 对。我没有意识到这一点。谢谢。
    • @user1526306 没有公开的方式来迭代容器中的 cookie,这肯定无济于事。 :(
    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 2012-10-13
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2011-10-14
    相关资源
    最近更新 更多