Utils.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Text;
  7. namespace CommonTool
  8. {
  9. public sealed class Utils
  10. {
  11. public static Image LoadImage(string pcFileName)
  12. {
  13. string text1 = FileFuns.GetFullPathFileName(pcFileName);
  14. try
  15. {
  16. return Image.FromFile(text1);
  17. }
  18. catch (Exception)
  19. {
  20. return null;
  21. }
  22. }
  23. public static void ReverseArray(Array array)
  24. {
  25. int count = array.Length;
  26. for (int i = 0; i < count / 2; i++)
  27. {
  28. var temp = array.GetValue(count - 1 - i);
  29. array.SetValue(array.GetValue(i), count - 1 - i);
  30. array.SetValue(temp, i);
  31. }
  32. }
  33. public static string AreaToSql(string pcStr)
  34. {
  35. pcStr = pcStr.Replace("'", "''");
  36. return pcStr;
  37. }
  38. public static string AreaToSqLcs(string pcStr)
  39. {
  40. pcStr = pcStr.Replace("'", "''");
  41. pcStr = pcStr.Replace("\"", "''''");
  42. return pcStr;
  43. }
  44. public static string CvFieldInSql(string inFieldName, string inField, string outField, string outFieldName)
  45. {
  46. string lcRetVal = "";
  47. Array arr1 = UtilStr.StrToArray(inField);
  48. Array arr2 = UtilStr.StrToArray(outField);
  49. if (outFieldName.Trim().Length == 0)
  50. {
  51. outFieldName = inFieldName;
  52. }
  53. if (arr1.Length == arr2.Length)
  54. {
  55. lcRetVal = " case " + inFieldName;
  56. for (int i = 0; i <= arr1.Length; i++)
  57. {
  58. lcRetVal += " when '" + arr1.GetValue(i) + "' then '" + arr2.GetValue(i) + "' ";
  59. }
  60. lcRetVal += " end as " + outFieldName;
  61. }
  62. if (inFieldName == "" || inField == "" || outField == "")
  63. {
  64. lcRetVal = "";
  65. }
  66. return lcRetVal;
  67. }
  68. public static string Decode(string pcKey)
  69. {
  70. pcKey = pcKey.Replace("*", "%");
  71. pcKey = System.Web.HttpUtility.UrlDecode(pcKey, Encoding.Default);
  72. return pcKey;
  73. }
  74. public static string Encode(string asKey)
  75. {
  76. asKey = System.Web.HttpUtility.UrlEncode(asKey, Encoding.Default);
  77. return asKey?.Replace("%", "*");
  78. }
  79. public static string IsHasSqlEsc(string pcCheckedStr)
  80. {
  81. if (pcCheckedStr.Trim().Length > 0 && pcCheckedStr.IndexOf("'", StringComparison.Ordinal) > 0)
  82. {
  83. return "'";
  84. }
  85. return "N";
  86. }
  87. public static bool ObjToBool(object poObj)
  88. {
  89. if (poObj == null)
  90. return false;
  91. return UtilStr.StrToBool(UtilStr.StrFromObj(poObj));
  92. }
  93. public string SmallDate(ref string pcDateField, string pcShowName)
  94. {
  95. if (pcShowName.Trim().Length == 0)
  96. {
  97. pcShowName = pcDateField;
  98. }
  99. return ("(convert(char(10)," + pcDateField + ",120)) as " + pcShowName);
  100. }
  101. //======================================= New Add=============================
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. /// <param name="poImage"></param>
  106. /// <returns></returns>
  107. public static byte[] BytesFromImage(Image poImage)
  108. {
  109. byte[] buffer1 = null;
  110. if (poImage != null)
  111. {
  112. MemoryStream stream1 = new MemoryStream();
  113. poImage.Save(stream1, ImageFormat.Bmp);
  114. buffer1 = new byte[stream1.Length];
  115. stream1.Seek(0, SeekOrigin.Begin);
  116. stream1.Read(buffer1, 0, (int)stream1.Length);
  117. stream1.Close();
  118. }
  119. return buffer1;
  120. }
  121. public static object BytesToImage(object poObject)
  122. {
  123. object obj1 = null;
  124. if ((poObject != null) && !(poObject is DBNull))
  125. {
  126. try
  127. {
  128. byte[] buffer1 = (byte[])poObject;
  129. if (buffer1.Length > 0)
  130. {
  131. MemoryStream stream1 = new MemoryStream(buffer1, true);
  132. stream1.Write(buffer1, 0, buffer1.Length);
  133. obj1 = Image.FromStream(stream1);
  134. stream1.Close();
  135. }
  136. }
  137. catch (Exception exception1)
  138. {
  139. obj1 = exception1.Message;
  140. }
  141. return obj1;
  142. }
  143. return "";
  144. }
  145. /// <summary>
  146. /// 将string型转换成 Int 型
  147. /// </summary>
  148. /// <param name="str"></param>
  149. /// <returns></returns>
  150. public static int ValI(string str)
  151. {
  152. return (int)ValD(str);
  153. }
  154. /// <summary>
  155. /// 将string型转换成 Int64 型
  156. /// </summary>
  157. /// <param name="str"></param>
  158. /// <returns></returns>
  159. public static Int64 ValI64(string str)
  160. {
  161. return (Int64)ValD(str);
  162. }
  163. /// <summary>
  164. /// 字符串转换成数值型
  165. /// </summary>
  166. /// <param name="str"></param>
  167. /// <returns></returns>
  168. public static decimal ValD(string str)
  169. {
  170. string text1 = "";
  171. foreach (char ch1 in str)
  172. {
  173. switch (ch1)
  174. {
  175. case ' ':
  176. case ',':
  177. break;
  178. default:
  179. if ((ch1 >= '0') && (ch1 <= '9'))
  180. {
  181. text1 = text1 + ch1.ToString();
  182. }
  183. else if (((ch1 == '+') || (ch1 == '-')) && (text1 == ""))
  184. {
  185. text1 = text1 + ch1.ToString();
  186. }
  187. else
  188. {
  189. if ((ch1 != '.') || (text1.IndexOf(ch1) >= 0))
  190. {
  191. break;
  192. }
  193. text1 = text1 + ch1.ToString();
  194. }
  195. break;
  196. }
  197. }
  198. if (text1 == "")
  199. {
  200. text1 = "0";
  201. }
  202. else if (text1 == "-")
  203. {
  204. text1 = "-0";
  205. }
  206. return Convert.ToDecimal(text1);
  207. }
  208. public static decimal ValD(object poObj)
  209. {
  210. return ValD(UtilStr.StrFromObj(poObj));
  211. }
  212. public static float ValSingle(object pcStr)
  213. {
  214. float lfRetVal = 0f;
  215. try
  216. {
  217. lfRetVal = Convert.ToSingle(pcStr);
  218. }
  219. catch
  220. {
  221. // ignored
  222. }
  223. return lfRetVal;
  224. }
  225. public static float PixelsToInch(Graphics g, int pixels, bool horizontal)
  226. {
  227. float single1 = horizontal ? g.DpiX : g.DpiY;
  228. return (pixels / single1);
  229. }
  230. public static string FormatCodeToXml(string pcString)
  231. {
  232. return pcString.Replace("<", ";lt;").Replace(">", ";gt;").Replace("&", ";amp;");
  233. }
  234. public static string FormatXmlToCode(string pcString)
  235. {
  236. return pcString.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">");
  237. }
  238. public static ArrayList ArrayToArrayList(Array paArr)
  239. {
  240. ArrayList list1 = new ArrayList();
  241. foreach (object obj1 in paArr)
  242. {
  243. list1.Add(obj1);
  244. }
  245. return list1;
  246. }
  247. public static bool IsNum(string pcStrs)
  248. {
  249. foreach (char pcstr in pcStrs)
  250. {
  251. if (pcstr <= '0' || pcstr >= '9')
  252. return false;
  253. }
  254. return true;
  255. }
  256. public static string ByteToString(byte[] inBytes)
  257. {
  258. string stringOut = "";
  259. foreach (byte inByte in inBytes)
  260. {
  261. stringOut = stringOut + $"{inByte:X2} ";
  262. }
  263. return stringOut;
  264. }
  265. public static string BytesToString(byte[] inBytes, Encoding encoding)
  266. {
  267. return encoding.GetString(inBytes);
  268. }
  269. public static byte[] StringToByte(string inString)
  270. {
  271. var byteStrings = inString.Split(" ".ToCharArray());
  272. var byteOut = new byte[byteStrings.Length - 1];
  273. for (int i = 0; i == byteStrings.Length - 1; i++)
  274. {
  275. byteOut[i] = Convert.ToByte(("0x" + byteStrings[i]));
  276. }
  277. return byteOut;
  278. }
  279. public static byte[] StringToBytes(string text, Encoding encoding)
  280. {
  281. //UTF8Encoding encoding = new UTF8Encoding();
  282. return encoding.GetBytes(text);
  283. }
  284. public static string GetQuerySql(string pcFiled, string pcValue)
  285. {
  286. return GetQuerySql(pcFiled, pcValue, false);
  287. }
  288. public static string GetQuerySql(string pcFiled, string pcValue, bool pbAddAnd)
  289. {
  290. string lcRetVal = "";
  291. if (pcFiled.Trim().Length > 0)
  292. {
  293. if (pbAddAnd)
  294. lcRetVal = " and ";
  295. lcRetVal += " " + pcFiled + " = '" + pcValue + "'";
  296. }
  297. return lcRetVal;
  298. }
  299. }
  300. public enum CovType
  301. {
  302. SqlQuary = 0,
  303. JsStr = 1,
  304. HtmlStr = 2,
  305. CtrlText = 3,
  306. UrlMsg = 4
  307. }
  308. }