using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace lvyou.Common
{
    /// <summary>
    /// Cookie 操作类
    /// </summary>
    public class CookieHelper
    {
        public CookieHelper() { }
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="values"></param>
        /// <param name="days"></param>
        public static void WriteCookie(string keys, string values, double days)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(keys, values);
                cookie.Expires = DateTime.Now.AddDays(days);
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
            catch (Exception)
            {
                //  throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 通过key读cookie
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ReadCookieByKey(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
            {
                return cookie.Value;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 清空
        /// </summary>
        public static void ClearCookie(string[] key)
        {
            foreach (string str in key)
            {
                if (HttpContext.Current.Request.Cookies[str] != null)
                {
                    HttpCookie cookie = new HttpCookie(str);
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                }
            }
        }
        /// <summary>
        /// 清空
        /// </summary>
        public static void ClearCookie(string str)
        {
            HttpCookie cookie = new HttpCookie(str);
            cookie.Expires = DateTime.Now.AddDays(-1);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static bool IsNum(string Str)
        {
            bool blResult = true;
            if (Str == "")
                blResult = false;
            else
            {
                foreach (char Char in Str)
                {
                    if (!Char.IsNumber(Char))
                    {
                        blResult = false;
                        break;
                    }
                }
                if (blResult)
                    if (int.Parse(Str) == 0)
                        blResult = false;
            }
            return blResult;
        }
       
 
    }
}