StringEx.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Text;
  8. namespace WWPipeLine.Commons
  9. {
  10. public class StringEx
  11. {
  12. public static string GetRandomString()
  13. {
  14. return GetRandomString(32, false, false, true, false, "");
  15. }
  16. ///<summary>
  17. ///生成随机字符串
  18. ///</summary>
  19. ///<param name="length">目标字符串的长度</param>
  20. ///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
  21. ///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
  22. ///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
  23. ///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
  24. ///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
  25. ///<returns>指定长度的随机字符串</returns>
  26. public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
  27. {
  28. byte[] b = new byte[4];
  29. new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
  30. Random r = new Random(BitConverter.ToInt32(b, 0));
  31. string s = null, str = custom;
  32. if (useNum == true) { str += "0123456789"; }
  33. if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
  34. if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
  35. if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
  36. for (int i = 0; i < length; i++)
  37. {
  38. s += str.Substring(r.Next(0, str.Length - 1), 1);
  39. }
  40. return s;
  41. }
  42. /// <summary>
  43. /// 字符串压缩
  44. /// </summary>
  45. /// <param name="strSource"></param>
  46. /// <returns></returns>
  47. public static byte[] Compress(byte[] data)
  48. {
  49. try
  50. {
  51. MemoryStream ms = new MemoryStream();
  52. GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
  53. zip.Write(data, 0, data.Length);
  54. zip.Close();
  55. byte[] buffer = new byte[ms.Length];
  56. ms.Position = 0;
  57. ms.Read(buffer, 0, buffer.Length);
  58. ms.Close();
  59. return buffer;
  60. }
  61. catch (Exception e)
  62. {
  63. throw new Exception(e.Message);
  64. }
  65. }
  66. /// <summary>
  67. /// 字符串解压缩
  68. /// </summary>
  69. /// <param name="strSource"></param>
  70. /// <returns></returns>
  71. public static byte[] Decompress(byte[] data)
  72. {
  73. try
  74. {
  75. MemoryStream ms = new MemoryStream(data);
  76. GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
  77. MemoryStream msreader = new MemoryStream();
  78. byte[] buffer = new byte[0x1000];
  79. while (true)
  80. {
  81. int reader = zip.Read(buffer, 0, buffer.Length);
  82. if (reader <= 0)
  83. {
  84. break;
  85. }
  86. msreader.Write(buffer, 0, reader);
  87. }
  88. zip.Close();
  89. ms.Close();
  90. msreader.Position = 0;
  91. buffer = msreader.ToArray();
  92. msreader.Close();
  93. return buffer;
  94. }
  95. catch (Exception e)
  96. {
  97. throw new Exception(e.Message);
  98. }
  99. }
  100. public static string CompressString(string str)
  101. {
  102. string compressString = "";
  103. byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);
  104. byte[] compressAfterByte = Compress(compressBeforeByte);
  105. //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);
  106. compressString = Convert.ToBase64String(compressAfterByte);
  107. return compressString;
  108. }
  109. public static string DecompressString(string str)
  110. {
  111. string compressString = "";
  112. //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);
  113. byte[] compressBeforeByte = Convert.FromBase64String(str);
  114. byte[] compressAfterByte = Decompress(compressBeforeByte);
  115. compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);
  116. return compressString;
  117. }
  118. /// <summary>
  119. /// 获取枚举描述信息
  120. /// </summary>
  121. /// <typeparam name="T"></typeparam>
  122. /// <param name="eunmObj"></param>
  123. /// <returns></returns>
  124. public static string GetEnumDescription<T>(T eunmObj)
  125. {
  126. //获取枚举对象的枚举类型
  127. var type = eunmObj.GetType();
  128. //通过反射获取该枚举类型的所有属性
  129. var fieldInfos = type.GetFields();
  130. foreach (var field in fieldInfos)
  131. {
  132. //不是参数obj,就直接跳过
  133. if (field.Name != eunmObj.ToString())
  134. {
  135. continue;
  136. }
  137. //取出参数obj的自定义属性
  138. if (!field.IsDefined(typeof(DescriptionAttribute), true)) continue;
  139. var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute),
  140. true)[0] as DescriptionAttribute;
  141. if (descriptionAttribute != null)
  142. return descriptionAttribute.Description;
  143. }
  144. return eunmObj.ToString();
  145. }
  146. public static List<string> ColorsToString(List<Color> colorList)
  147. {
  148. List<string> RGBList = new List<string>();
  149. foreach (Color color in colorList)
  150. {
  151. RGBList.Add(color.R.ToString() + "," + color.G.ToString() + "," + color.B.ToString());
  152. }
  153. return RGBList;
  154. }
  155. }
  156. }