码迷,mamicode.com
首页 > Web开发 > 详细

.Net常用工具类——Session操作类

时间:2015-07-18 13:55:45      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ZC.Utils
{
    /// <summary>
    /// <para> </para>
    ///  常用工具类——Session操作类
    /// <para> ----------------------------------------------------------</para>
    /// <para> AddSession:添加Session,有效期为默认</para>
    /// <para> AddSession:添加Session,并调整有效期为分钟或几年</para>
    /// <para> GetSession:读取某个Session对象值</para>
    /// <para> DelSession:删除某个Session对象</para>
    /// </summary>
    public class Session
    {

        public object this[string key]
        {
            get
            {
                return Get(key);
            }
            set
            {
                Set(key, value, 60, 0);
            }
        }

       

        #region 添加Session,有效期为默认
        /// <summary>
        /// 添加Session,有效期为默认
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <param name="strValue">Session值</param>
        public static void Add(string strSessionName, object objValue)
        {
            HttpContext.Current.Session[strSessionName] = objValue;
        }
        #endregion

        #region 添加Session,并调整有效期为分钟或几年
        /// <summary>
        /// 添加Session,并调整有效期为分钟或几年
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <param name="strValue">Session值</param>
        /// <param name="iExpires">分钟数:大于0则以分钟数为有效期,等于0则以后面的年为有效期</param>
        /// <param name="iYear">年数:当分钟数为0时按年数为有效期,当分钟数大于0时此参数随意设置</param>
        public static void Set(string strSessionName, object objValue, int iExpires, int iYear)
        {
            HttpContext.Current.Session[strSessionName] = objValue;
            if (iExpires > 0)
            {
                HttpContext.Current.Session.Timeout = iExpires;
            }
            else if(iYear>0)
            {
                HttpContext.Current.Session.Timeout = 60 * 24 * 365 * iYear;
            }
        }
        #endregion

        #region 读取某个Session对象值
        /// <summary>
        /// 读取某个Session对象值
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        /// <returns>Session对象值</returns>
        public static object Get(string strSessionName)
        {
            return HttpContext.Current.Session[strSessionName];
        }
        #endregion

        #region 删除某个Session对象
        /// <summary>
        /// 删除某个Session对象
        /// </summary>
        /// <param name="strSessionName">Session对象名称</param>
        public static void Remove(string strSessionName)
        {
            HttpContext.Current.Session.Remove(strSessionName);
        }
        #endregion
    }
}

.Net常用工具类——Session操作类

标签:

原文地址:http://www.cnblogs.com/huangdegen/p/4656695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!