日期:2013-12-24  浏览次数:20779 次

  
//注意,需要引用System.Runtime.Serialization.Formatters.Soap.dll程序集 
public const string SESSIONDATAPATH = "C:\SessionData\" ;
private void Application_AcquireRequestState( object sender, EventArgs e)                           
 {     
       System.IO.FileStream fs; 
        System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf  =  new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); 
        try 
        {  
            //获取特定的cookie,如果找不到,则退出.
            HttpCookie cookie = Request.Cookies["PermSessionID"]; 
            if(cookie == null) 
            {
                //如果找不到,则生成一个(使用伪随机的SessionID) 
               cookie = new HttpCookie("PermSessionID", Session.SessionID);
               //使该cookie在1星期之后到期
               cookie.Expires = DateTime.Now.AddDays(7);
               //将其发往客户端浏览器 
               Response.Cookies.Add(cookie); 
            }
             //文件名等于该cookie的值 
             string permSessionId = cookie.Value; 
             //生成数据文件的名称 
            string filename = SESSIONDATAPATH + permSessionId.ToString() + ".xml"; 
            //打开文件,如果出错,则退出 
            fs = new System.IO.FileStream(filename, IO.FileMode.Open); 
            //反序列化包含值的Hashtable Hashtable ht = (Hashtable)sf.Deserialize(fs); 
            //将数据移到Session集合中 
            Session.Clear(); 
            foreach( string key in ht.Keys )
            {
                Session(key) = ht(key); 
            }
      } 
     Catch(Exception e