标签:exception 默认值 cep move count manage static 设置 time
 public class Session_Manager
    {
        /// <summary>
        /// 获取Session对象值
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static object Get(string Key)
        {
            return HttpContext.Current.Session[Key];
        }
        /// <summary>
        /// 获取Session对象值
        /// </summary>
        /// <param name="Key">Session对象名称</param>
        /// <returns></returns>
        public static string GetString(string Key)
        {
            object obj = HttpContext.Current.Session[Key];
            if (obj == null) return "";
            else return obj.ToString();
        }
        /// <summary>
        /// 获取Session对象值(当缓存为空时取默认值)
        /// </summary>
        /// <param name="Key">Session对象名称</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public static object Get(string Key, object DefaultValue)
        {
            if (HttpContext.Current.Session[Key] == null)
                return DefaultValue;
            else
                return HttpContext.Current.Session[Key];
}
        /// <summary>
        /// Session对象值
        /// </summary>
        /// <param name="Key">Session对象名称</param>
        /// <param name="DefaultValue">默认缓存值</param>
        /// <param name="CanAdd">是否取默认缓存值(true,false)</param>
        /// <returns></returns>
        public static object Get(string Key, object DefaultValue, Boolean CanAdd)
        {
            if (HttpContext.Current.Session[Key] == null)
            {
                if (CanAdd == true)
                    HttpContext.Current.Session.Add(Key, DefaultValue);
                return DefaultValue;
            }
            else
                return HttpContext.Current.Session[Key];
}
        /// <summary>
        /// 添加Session默认有效期
        /// </summary>
        /// <param name="Key">Session对象名称</param>
        /// <param name="Value">Session值</param>
        /// <returns></returns>
        public static Boolean Set(string Key, object Value)
        {
            try
            {
                if (Value == null && HttpContext.Current.Session[Key] != null)
                {
                    HttpContext.Current.Session.Remove(Key);
                }
                else if (HttpContext.Current.Session[Key] == null)
                {
                    HttpContext.Current.Session.Add(Key, Value);
                }
                else
                {
                    HttpContext.Current.Session[Key] = Value;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #region 添加Session,并调整有效期为分钟或几年
        /// <summary>
        /// 添加Session,并调整有效期为分钟或几年
        /// </summary>
        /// <param name="Key">Session对象名称</param>
        /// <param name="objValue">Session值</param>
        /// <param name="MinuteCount">分钟数:大于0则以分钟数为有效期,等于0则以后面的年为有效期</param>
        /// <param name="Yearcount">年数:当分钟数为0时按年数为有效期,当分钟数大于0时此参数随意设置</param>
        public static void Set(string Key, object objValue, int MinuteCount, int Yearcount)
        {
            HttpContext.Current.Session[Key] = objValue;
            if (MinuteCount> 0)
            {
                HttpContext.Current.Session.Timeout = MinuteCount;
            }
            else if (Yearcount> 0)
            {
                HttpContext.Current.Session.Timeout = 60 * 24 * 365 * Yearcount;
            }
        }
        /// <summary>
        /// 删除某个Session对象
        /// </summary>
        /// <param name="key">Session对象名称</param>
        public static void Remove(string key)
        {
            HttpContext.Current.Session.Remove(key);
        }
    }
标签:exception 默认值 cep move count manage static 设置 time
原文地址:https://www.cnblogs.com/dulang/p/9284903.html