CookContainer 序列化保存

using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography.X509Certificates;

public static bool WriteCookiesToDisk(string file, CookieContainer cookieJar)
{
//file = file.Replace(":", ":");
if (file == "") return false ;
using (Stream stream = File.Create(file))
{
try
{
//Console.Out.Write("Writing cookies to disk... ");
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, cookieJar);
//Console.Out.WriteLine("Done.");
return true;
}
catch (Exception e)
{
// Console.Out.WriteLine("Problem writing cookies to disk: " + e.GetType());
}
}
return false;
}

public static CookieContainer ReadCookiesFromDisk(string file)
{
// file = file.Replace(":", ":");
try
{
using (Stream stream = File.Open(file, FileMode.Open))
{
// Console.Out.Write("Reading cookies from disk... ");
BinaryFormatter formatter = new BinaryFormatter();
// Console.Out.WriteLine("Done.");
return (CookieContainer)formatter.Deserialize(stream);
}
}
catch (Exception e)
{
// Console.Out.WriteLine("Problem reading cookies from disk: " + e.GetType());
//return new CookieContainer();
return new CookieContainer();
}
}

上一篇:[Aaronyang] 写给自己的WPF4.5 笔记12[自定义控件-AyImageButton的过程 2/4]


下一篇:(转)使用SQLCMD在SQLServer执行多个脚本