C# Session添加、删除封装类

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

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

        #region 添加Session,并调整有效期为分钟或几年
        /// 
        /// 添加Session,并调整有效期为分钟或几年
        /// 
        /// Session对象名称
        /// Session值
        /// 分钟数:大于0则以分钟数为有效期,等于0则以后面的年为有效期
        /// 年数:当分钟数为0时按年数为有效期,当分钟数大于0时此参数随意设置
        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对象值
        /// 
        /// 读取某个Session对象值
        /// 
        /// Session对象名称
        /// Session对象值
        public static object Get(string strSessionName)
        {
            return HttpContext.Current.Session[strSessionName];
        }
        #endregion

        #region 删除某个Session对象
        /// 
        /// 删除某个Session对象
        /// 
        /// Session对象名称
        public static void Remove(string strSessionName)
        {
            HttpContext.Current.Session.Remove(strSessionName);
        }
        #endregion
    }

分装后使用更方便快捷 

你可能感兴趣的:(C# Session添加、删除封装类)