C#操作session的类实例
本文实例讲述了C#操作session的类。分享给大家供大家参考。具体分析如下:
这个C#类对session操作进行了再次封装,可以大大简化session的常用操作,同时这个类可以将session值设置为数组,也可以将值读取为数组列表,如果你有这方面的需要可以使用这个类,扩这自己对这个C#类进行扩展。
usingSystem.Web;
namespaceDotNet.Utilities
{
publicstaticclassSessionHelper2
{
///<summary>
///添加Session,调动有效期为20分钟
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValue">Session值</param>
publicstaticvoidAdd(stringstrSessionName,stringstrValue)
{
HttpContext.Current.Session[strSessionName]=strValue;
HttpContext.Current.Session.Timeout=20;
}
///<summary>
///添加Session,调动有效期为20分钟
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValues">Session值数组</param>
publicstaticvoidAdds(stringstrSessionName,string[]strValues)
{
HttpContext.Current.Session[strSessionName]=strValues;
HttpContext.Current.Session.Timeout=20;
}
///<summary>
///添加Session
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValue">Session值</param>
///<paramname="iExpires">调动有效期(分钟)</param>
publicstaticvoidAdd(stringstrSessionName,stringstrValue,intiExpires)
{
HttpContext.Current.Session[strSessionName]=strValue;
HttpContext.Current.Session.Timeout=iExpires;
}
///<summary>
///添加Session
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<paramname="strValues">Session值数组</param>
///<paramname="iExpires">调动有效期(分钟)</param>
publicstaticvoidAdds(stringstrSessionName,string[]strValues,intiExpires)
{
HttpContext.Current.Session[strSessionName]=strValues;
HttpContext.Current.Session.Timeout=iExpires;
}
///<summary>
///读取某个Session对象值
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<returns>Session对象值</returns>
publicstaticstringGet(stringstrSessionName)
{
if(HttpContext.Current.Session[strSessionName]==null)
{
returnnull;
}
else
{
returnHttpContext.Current.Session[strSessionName].ToString();
}
}
///<summary>
///读取某个Session对象值数组
///</summary>
///<paramname="strSessionName">Session对象名称</param>
///<returns>Session对象值数组</returns>
publicstaticstring[]Gets(stringstrSessionName)
{
if(HttpContext.Current.Session[strSessionName]==null)
{
returnnull;
}
else
{
return(string[])HttpContext.Current.Session[strSessionName];
}
}
///<summary>
///删除某个Session对象
///</summary>
///<paramname="strSessionName">Session对象名称</param>
publicstaticvoidDel(stringstrSessionName)
{
HttpContext.Current.Session[strSessionName]=null;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。