SessionUtil.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. namespace CP.Common
  6. {
  7. public static class SessionUtil
  8. {
  9. /// <summary>
  10. /// 添加Session,调动有效期为20分钟
  11. /// </summary>
  12. /// <param name="strSessionName">Session对象名称</param>
  13. /// <param name="strValue">Session值</param>
  14. public static void Insert(string strSessionName, string strValue)
  15. {
  16. HttpContext.Current.Session[strSessionName] = strValue;
  17. HttpContext.Current.Session.Timeout = 20;
  18. }
  19. /// <summary>
  20. /// 添加Session,调动有效期为20分钟
  21. /// </summary>
  22. /// <param name="strSessionName">Session对象名称</param>
  23. /// <param name="strValues">Session值数组</param>
  24. public static void Inserts(string strSessionName, string[] strValues)
  25. {
  26. HttpContext.Current.Session[strSessionName] = strValues;
  27. HttpContext.Current.Session.Timeout = 20;
  28. }
  29. /// <summary>
  30. /// 添加Session
  31. /// </summary>
  32. /// <param name="strSessionName">Session对象名称</param>
  33. /// <param name="strValue">Session值</param>
  34. /// <param name="iExpires">调动有效期(分钟)</param>
  35. public static void Insert(string strSessionName, string strValue, int iExpires)
  36. {
  37. HttpContext.Current.Session[strSessionName] = strValue;
  38. HttpContext.Current.Session.Timeout = iExpires;
  39. }
  40. /// <summary>
  41. /// 添加Session
  42. /// </summary>
  43. /// <param name="strSessionName">Session对象名称</param>
  44. /// <param name="strValues">Session值数组</param>
  45. /// <param name="iExpires">调动有效期(分钟)</param>
  46. public static void Inserts(string strSessionName, string[] strValues, int iExpires)
  47. {
  48. HttpContext.Current.Session[strSessionName] = strValues;
  49. HttpContext.Current.Session.Timeout = iExpires;
  50. }
  51. /// <summary>
  52. /// 读取某个Session对象值
  53. /// </summary>
  54. /// <param name="strSessionName">Session对象名称</param>
  55. /// <returns>Session对象值</returns>
  56. public static string Read(string strSessionName)
  57. {
  58. if (HttpContext.Current.Session[strSessionName] == null)
  59. {
  60. return null;
  61. }
  62. else
  63. {
  64. return HttpContext.Current.Session[strSessionName].ToString();
  65. }
  66. }
  67. /// <summary>
  68. /// 读取某个Session对象值数组
  69. /// </summary>
  70. /// <param name="strSessionName">Session对象名称</param>
  71. /// <returns>Session对象值数组</returns>
  72. public static string[] Reades(string strSessionName)
  73. {
  74. if (HttpContext.Current.Session[strSessionName] == null)
  75. {
  76. return null;
  77. }
  78. else
  79. {
  80. return (string[])HttpContext.Current.Session[strSessionName];
  81. }
  82. }
  83. /// <summary>
  84. /// 删除某个Session对象
  85. /// </summary>
  86. /// <param name="strSessionName">Session对象名称</param>
  87. public static void Remove(string strSessionName)
  88. {
  89. HttpContext.Current.Session[strSessionName] = null;
  90. }
  91. }
  92. }