C#程序中session的基本设置示例及清除session的方法
session的基本设置:
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Web; usingSystem.Web.SessionState; namespaceOAFrameWork { publicclassCSession { publicstaticobjectGet(stringKey) { returnHttpContext.Current.Session[Key]; } publicstaticstringGetString(stringKey) { objectobj=HttpContext.Current.Session[Key]; if(obj==null)return""; elsereturnobj.ToString(); } publicstaticobjectGet(stringKey,objectDefaultValue) { if(HttpContext.Current.Session[Key]==null) returnDefaultValue; else returnHttpContext.Current.Session[Key]; } publicstaticobjectGet(stringKey,objectDefaultValue,BooleanCanAdd) { if(HttpContext.Current.Session[Key]==null) { if(CanAdd==true) HttpContext.Current.Session.Add(Key,DefaultValue); returnDefaultValue; } else returnHttpContext.Current.Session[Key]; } publicstaticBooleanSet(stringKey,objectValue) { try { if(Value==null&&HttpContext.Current.Session[Key]!=null) { HttpContext.Current.Session.Remove(Key); } elseif(HttpContext.Current.Session[Key]==null) { HttpContext.Current.Session.Add(Key,Value); } else { HttpContext.Current.Session[Key]=Value; } returntrue; } catch(Exceptionex) { CMsgBox.Show(ex.Message); returnfalse; } } } }
清除Session:
Session.Abandon();//清除全部Session //清除某个Session Session["UserName"]=null; Session.Remove("UserName");