转自于:http://www.cnblogs.com/fooo/archive/2008/01/07/1029119.html
//-----------------------------载入
if(!IsPostBack){
HttpCookie cookie = Request.Cookies["userinfo1"];
cookie.Expires = System.DateTime.Now.AddMinutes(20);//设置过期时间
for (int i = 0; i < 5; i++)
{
cookie.Values["BB" + i.ToString()] = i.ToString();
Response.Cookies.Add(cookie);
}
}
//**************写入
HttpCookie cookie = new HttpCookie("userinfo1");
cookie.Expires = System.DateTime.Now.AddMinutes(20);//设置过期时间
for (int i = 0; i < 10; i++)
{
cookie.Values["U" +i.ToString()] = i.ToString();
Response.Cookies.Add(cookie);
}
//**************---------------读取
//读取 Cookie 集合
for (int i = 0; i < Request.Cookies.Count; i++)
{
if (Request.Cookies.AllKeys[i] == "userinfo1")
{
HttpCookie cookies = Request.Cookies["userInfo1"];
Response.Write("name=" + cookies.Name + "<br/>");
//Response.Write("name=" + cookies.Value + "<br/>");
if (cookies.HasKeys)//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl = cookies.Values;
for (int j = 0; j < NameColl.Count; j++)
{
Response.Write("子键名=" + NameColl.AllKeys[j] + "<br/>");
Response.Write("子键值=" + NameColl[j] + "<br/>");
}
}
else
{
Response.Write("value=" + cookies.Value + "<br/>");
}
}
}
//***********************删除
HttpCookie acookie = Request.Cookies["userinfo1"];
acookie.Expires = System.DateTime.Now.AddMinutes(20);//设置过期时间
if (acookie.HasKeys)//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl = acookie.Values;
for (int j = 0 ; j < NameColl.Count; j++)
{
if (NameColl.AllKeys[j] == "U8")
{
acookie.Values.Remove(NameColl.AllKeys[j]);
Response.Cookies.Add(acookie);
}
}
}
//***************************追加
HttpCookie cookie = Request.Cookies["userinfo1"];
cookie.Expires = System.DateTime.Now.AddMinutes(20);//设置过期时间
for (int i = 0; i < 5; i++)
{
cookie.Values["K" + i.ToString()] = i.ToString();
Response.Cookies.Add(cookie);
}
---------
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cookie=new HttpCookie("MyCook");//初使化并设置Cookie的名称
DateTime dt=DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 1,0,0);//过期时间为1分钟
cookie.Expires = dt.Add(ts);//设置过期时间
cookie.Values.Add("userid", "userid_value");
cookie.Values.Add("userid2","userid2_value2");
Response.AppendCookie(cookie);
//输出该Cookie的所有内容
//Response.Write(cookie.Value); //输出为:userid=userid_value&userid2=userid2_value2
}
//读取
protected void Button1_Click(object sender, EventArgs e)
{
// HttpCookie cokie = new HttpCookie("MyCook");//初使化
if (Request.Cookies["MyCook"]!=null)
{
//Response.Write("Cookie中键值为userid的值:" + Request.Cookies["MyCook"]["userid"]);//整行
//Response.Write("Cookie中键值为userid2的值" + Request.Cookies["MyCook"]["userid2"]);
Response.Write(Request.Cookies["MyCook"].Value);//输出全部的值
}
}
//修改Cookie
protected void Button3_Click(object sender, EventArgs e)
{
//获取客户端的Cookie对象
HttpCookie cok = Request.Cookies["MyCook"];
if (cok != null)
{
//修改Cookie的两种方法
cok.Values["userid"] = "alter-value";
cok.Values.Set("userid", "alter-value");
//往Cookie里加入新的内容
cok.Values.Set("newid", "newValue");
Response.AppendCookie(cok);
}
}
//删除Cookie
protected void Button4_Click(object sender, EventArgs e)
{
HttpCookie cok = Request.Cookies["MyCook"];
if (cok != null)
{
if (!CheckBox1.Checked)
{
cok.Values.Remove("userid");//移除键值为userid的值
}
else
{
TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
cok.Expires = DateTime.Now.Add(ts);//删除整个Cookie,只要把过期时间设置为现在
}
Response.AppendCookie(cok);
}
}
-------------------------------------
什么是Cookie