2012-07-15 54 views
3

基本上,我需要知道如何在.net 4.5中編寫此代碼。我無法在MSDN中找到它。如何將cookie保存到Metro中的文件中?

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的替代品。我無法弄清楚如何序列化文件的數據。

回答

2

您可以使用MemoryStream將數據作爲字節數組獲取,然後您可以將其保存到StorageFile中。

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

謝謝,但問題是序列化。出於某種原因,BinaryFormatter不存在於metro runtime.serialization庫中,並且xmlserializer沒有正確地序列化cookie容器對象。 – Smeegs 2012-07-15 14:38:07

+0

@ user1526306對。我沒有意識到這一點。謝謝。 – 2012-07-15 15:59:45

+0

@ user1526306它確實沒有幫助,沒有公開的方式來遍歷容器中的cookie。 :( – 2012-07-15 16:18:15

相關問題