using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; using System.IO.Compression; using System.Text; namespace WWPipeLine.Commons { public class StringEx { public static string GetRandomString() { return GetRandomString(32, false, false, true, false, ""); } /// ///生成随机字符串 /// ///目标字符串的长度 ///是否包含数字,1=包含,默认为包含 ///是否包含小写字母,1=包含,默认为包含 ///是否包含大写字母,1=包含,默认为包含 ///是否包含特殊字符,1=包含,默认为不包含 ///要包含的自定义字符,直接输入要包含的字符列表 ///指定长度的随机字符串 public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom) { byte[] b = new byte[4]; new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); Random r = new Random(BitConverter.ToInt32(b, 0)); string s = null, str = custom; if (useNum == true) { str += "0123456789"; } if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; } if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; } for (int i = 0; i < length; i++) { s += str.Substring(r.Next(0, str.Length - 1), 1); } return s; } /// /// 字符串压缩 /// /// /// public static byte[] Compress(byte[] data) { try { MemoryStream ms = new MemoryStream(); GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true); zip.Write(data, 0, data.Length); zip.Close(); byte[] buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } /// /// 字符串解压缩 /// /// /// public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true); MemoryStream msreader = new MemoryStream(); byte[] buffer = new byte[0x1000]; while (true) { int reader = zip.Read(buffer, 0, buffer.Length); if (reader <= 0) { break; } msreader.Write(buffer, 0, reader); } zip.Close(); ms.Close(); msreader.Position = 0; buffer = msreader.ToArray(); msreader.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } public static string CompressString(string str) { string compressString = ""; byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressAfterByte = Compress(compressBeforeByte); //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); compressString = Convert.ToBase64String(compressAfterByte); return compressString; } public static string DecompressString(string str) { string compressString = ""; //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressBeforeByte = Convert.FromBase64String(str); byte[] compressAfterByte = Decompress(compressBeforeByte); compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); return compressString; } /// /// 获取枚举描述信息 /// /// /// /// public static string GetEnumDescription(T eunmObj) { //获取枚举对象的枚举类型 var type = eunmObj.GetType(); //通过反射获取该枚举类型的所有属性 var fieldInfos = type.GetFields(); foreach (var field in fieldInfos) { //不是参数obj,就直接跳过 if (field.Name != eunmObj.ToString()) { continue; } //取出参数obj的自定义属性 if (!field.IsDefined(typeof(DescriptionAttribute), true)) continue; var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), true)[0] as DescriptionAttribute; if (descriptionAttribute != null) return descriptionAttribute.Description; } return eunmObj.ToString(); } public static List ColorsToString(List colorList) { List RGBList = new List(); foreach (Color color in colorList) { RGBList.Add(color.R.ToString() + "," + color.G.ToString() + "," + color.B.ToString()); } return RGBList; } } }