Utility.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Web;
  3. //using System.Web.UI;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Collections.Generic;
  7. namespace ZcPeng.PublicLibrary
  8. {
  9. /// <summary>
  10. /// Utility
  11. /// 功能:工具类。
  12. /// 作者:彭昭成
  13. /// 时间:2018年12月10日
  14. /// </summary>
  15. public static class Utility
  16. {
  17. //静态成员
  18. public static string ShowMessageUrl = "~/ShowMessage.aspx"; //“显示信息”页的url
  19. /// <summary>
  20. /// 生成”显示信息“页的含参数地址
  21. /// </summary>
  22. /// <param name="title">标题</param>
  23. /// <param name="description">描述</param>
  24. /// <returns>返回生成的url</returns>
  25. //public static string GenerateShowMessageUrl(string title, string description)
  26. //{
  27. // HttpServerUtility server = HttpContext.Current.Server;
  28. // return ShowMessageUrl + "?Title=" + server.UrlEncode(title) + "&Description=" + server.UrlEncode(description);
  29. //}
  30. /// <summary>
  31. /// 对指定字符串进行MD5加密(采用.net的方式)
  32. /// </summary>
  33. /// <param name="source">需要被加密的字符串</param>
  34. /// <param name="source">输出参数,返回加密之后的结果</param>
  35. /// <returns>返回是否加密成功</returns>
  36. public static bool MD5(string source, out string result)
  37. {
  38. bool bSuccessed = false;
  39. result = "";
  40. try
  41. {
  42. byte[] byteSource = Encoding.Default.GetBytes(source); //使用默认的ANSI编码
  43. System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider(); //创建MD5对象
  44. byte[] byteHash = md5.ComputeHash(byteSource); //进行HASH运算
  45. result = BitConverter.ToString(byteHash); //将得到的结果转化为字符串
  46. result = result.Replace("-", ""); //去掉结果中的减号“-”(以下2步是为了和ASP模式下的MD5兼容而设置)
  47. result = result.ToLower(); //将结果转化为小写
  48. md5.Clear();
  49. bSuccessed = true;
  50. }
  51. catch
  52. {
  53. bSuccessed = false;
  54. }
  55. if (!bSuccessed)
  56. result = "";
  57. return bSuccessed;
  58. }
  59. /// <summary>
  60. /// 显示客户端提示对话框信息
  61. /// </summary>
  62. /// <param name="scriptKey">脚本的键-用于避免重复注册客户端脚本</param>
  63. /// <param name="alertText">提示文本</param>
  64. //public static void ShowClientAlert(Page page, string scriptKey, string alertText)
  65. //{
  66. // alertText = alertText.Replace("\"", "\\\"");
  67. // ClientScriptManager cs = page.ClientScript;
  68. // if (!cs.IsStartupScriptRegistered(cs.GetType(), scriptKey))
  69. // cs.RegisterStartupScript(cs.GetType(), scriptKey, string.Format("<script language=\"javascript\" type=\"text/javascript\">alert(\"{0}\");</script>\r\n", alertText));
  70. //}
  71. /// <summary>
  72. /// 显示客户端是与否的对话框
  73. /// </summary>
  74. /// <param name="scriptKey">脚本的键-用于避免重复注册客户端脚本</param>
  75. /// <param name="alertText">提示文本</param>
  76. //public static void ShowConfirm(Page page, string scriptKey, string alertText, string doText)
  77. //{
  78. // alertText = alertText.Replace("\"", "\\\"");
  79. // ClientScriptManager cs = page.ClientScript;
  80. // if (!cs.IsStartupScriptRegistered(cs.GetType(), scriptKey))
  81. // cs.RegisterStartupScript(cs.GetType(), scriptKey, string.Format("<script>if(window.confirm('{0}')){1}</script>", alertText, doText));
  82. //}
  83. /// <summary>
  84. /// 得到GUID字符串,去掉“-”号,转换成大写字母
  85. /// </summary>
  86. /// <param name="guid">guid对象</param>
  87. /// <returns>返回GUID字符串</returns>
  88. public static string GetGuidString(Guid guid)
  89. {
  90. return guid.ToString().Replace("-", "").ToUpper();
  91. }
  92. /// <summary>
  93. /// 得到一个新的GUID字符串,去掉“-”号,转换成大写字母
  94. /// </summary>
  95. /// <returns>返回GUID字符串</returns>
  96. public static string GetNewGuidString()
  97. {
  98. return GetGuidString(Guid.NewGuid());
  99. }
  100. /// <summary>
  101. /// 从ORACLE数据库的BLOB类型中获取字符串
  102. /// </summary>
  103. /// <param name="blob">BLOB类型的值</param>
  104. /// <returns>返回字符串</returns>
  105. public static string GetStringFromOracleBlob(object blob)
  106. {
  107. return (blob == null || Convert.IsDBNull(blob)) ? "" : UTF8Encoding.UTF8.GetString((byte[])blob);
  108. }
  109. /// <summary>
  110. /// 从字符串中获取ORACLE数据库的BLOB类型
  111. /// </summary>
  112. /// <param name="str">字符串</param>
  113. /// <returns>返回字节数组</returns>
  114. public static byte[] GetOracleBlobFromString(string str)
  115. {
  116. return string.IsNullOrEmpty(str) ? null : UTF8Encoding.UTF8.GetBytes(str);
  117. }
  118. /// <summary>
  119. /// 判断列表中是否存在重复的元素
  120. /// </summary>
  121. /// <typeparam name="T">列表的类型,必须为实现了IComparable接口的可比较类型</typeparam>
  122. /// <param name="list">列表</param>
  123. /// <returns>返回是否包含重复元素</returns>
  124. public static bool IsRepeated<T>(List<T> list) where T : IComparable<T>
  125. {
  126. if (list != null && list.Count > 1)
  127. {
  128. int count = list.Count;
  129. for (int i = 0; i < count; i++)
  130. {
  131. for (int j = i + 1; j < count; j++)
  132. {
  133. if (list[i].CompareTo(list[j]) == 0)
  134. return true;
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. }
  141. }