今天有空就把操作cookIE的寫了,雖然很簡單,不過免得到時候忘記了,之前就是忘記了還很實驗了一番才弄出來,郁悶了。
下面是寫cookIE
1 HttpCookie cookie = new HttpCookie("Info");//定義cookIE對象以及名為Info的項
2 DateTime dt = DateTime.Now;//定義時間對象
3 TimeSpan ts=new TimeSpan(1,0,0,0);//cookIE有效作用時間,具體查msdn
4 cookIE.Expires = dt.Add(ts);//添加作用時間
5 cookIE.Values.Add("user","cxbkkk");//增加屬性
6 cookIE.Values.Add("userid","1203");
7 Response.AppendCookie(cookie);//確定寫入cookie中 讀取cookIE
1 if(Request.CookIEs["Info"]!=null)
2 {
3 string temp=Convert.ToString(Request.Cookies["Info"].Values["user"])+" "+Convert.ToString(Request.CookIEs["Info"].Values["userid"]);
4 //讀全部就用Request.CookIEs["Info"].Value)
5 if(temp=="")
6 {
7 Response.Write("空");
8 }
9 else
10 Response.Write(temp);
11 }
12 else
13 {
14 Response.Write("error");
15 } 修改cookIE
1 Response.CookIEs["Info"]["user"] = "2";
2 Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1); 刪除cookIE下的屬性
1 HttpCookie acookie=Request.CookIEs["Info"];
2 acookIE.Values.Remove("userid");
3 acookIE.Expires = DateTime.Now.AddDays(1);
4 Response.Cookies.Add(acookie); 刪除所有cookIE,就是設置過期時間為現在就行了
1 int limit=Request.CookIEs.Count - 1;
2 for(int i=0;i<limit;i++)
3 {
4 acookie = Request.CookIEs(i)
5 acookIE.Expires = DateTime.Now.AddDays(-1)
6 Response.Cookies.Add(acookIE)
7 } 這下不用到處找了