| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- using System;
- using System.Collections;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Text;
- namespace CommonTool
- {
- public sealed class Utils
- {
- public static Image LoadImage(string pcFileName)
- {
- string text1 = FileFuns.GetFullPathFileName(pcFileName);
- try
- {
- return Image.FromFile(text1);
- }
- catch (Exception)
- {
- return null;
- }
- }
- public static void ReverseArray(Array array)
- {
- int count = array.Length;
- for (int i = 0; i < count / 2; i++)
- {
- var temp = array.GetValue(count - 1 - i);
- array.SetValue(array.GetValue(i), count - 1 - i);
- array.SetValue(temp, i);
- }
- }
- public static string AreaToSql(string pcStr)
- {
- pcStr = pcStr.Replace("'", "''");
- return pcStr;
- }
- public static string AreaToSqLcs(string pcStr)
- {
- pcStr = pcStr.Replace("'", "''");
- pcStr = pcStr.Replace("\"", "''''");
- return pcStr;
- }
- public static string CvFieldInSql(string inFieldName, string inField, string outField, string outFieldName)
- {
- string lcRetVal = "";
- Array arr1 = UtilStr.StrToArray(inField);
- Array arr2 = UtilStr.StrToArray(outField);
- if (outFieldName.Trim().Length == 0)
- {
- outFieldName = inFieldName;
- }
- if (arr1.Length == arr2.Length)
- {
- lcRetVal = " case " + inFieldName;
- for (int i = 0; i <= arr1.Length; i++)
- {
- lcRetVal += " when '" + arr1.GetValue(i) + "' then '" + arr2.GetValue(i) + "' ";
- }
- lcRetVal += " end as " + outFieldName;
- }
- if (inFieldName == "" || inField == "" || outField == "")
- {
- lcRetVal = "";
- }
- return lcRetVal;
- }
- public static string Decode(string pcKey)
- {
- pcKey = pcKey.Replace("*", "%");
- pcKey = System.Web.HttpUtility.UrlDecode(pcKey, Encoding.Default);
- return pcKey;
- }
- public static string Encode(string asKey)
- {
- asKey = System.Web.HttpUtility.UrlEncode(asKey, Encoding.Default);
- return asKey?.Replace("%", "*");
-
- }
- public static string IsHasSqlEsc(string pcCheckedStr)
- {
- if (pcCheckedStr.Trim().Length > 0 && pcCheckedStr.IndexOf("'", StringComparison.Ordinal) > 0)
- {
- return "'";
- }
- return "N";
- }
- public static bool ObjToBool(object poObj)
- {
- if (poObj == null)
- return false;
- return UtilStr.StrToBool(UtilStr.StrFromObj(poObj));
- }
- public string SmallDate(ref string pcDateField, string pcShowName)
- {
- if (pcShowName.Trim().Length == 0)
- {
- pcShowName = pcDateField;
- }
- return ("(convert(char(10)," + pcDateField + ",120)) as " + pcShowName);
- }
- //======================================= New Add=============================
- /// <summary>
- ///
- /// </summary>
- /// <param name="poImage"></param>
- /// <returns></returns>
- public static byte[] BytesFromImage(Image poImage)
- {
- byte[] buffer1 = null;
- if (poImage != null)
- {
- MemoryStream stream1 = new MemoryStream();
- poImage.Save(stream1, ImageFormat.Bmp);
- buffer1 = new byte[stream1.Length];
- stream1.Seek(0, SeekOrigin.Begin);
- stream1.Read(buffer1, 0, (int)stream1.Length);
- stream1.Close();
- }
- return buffer1;
- }
- public static object BytesToImage(object poObject)
- {
- object obj1 = null;
- if ((poObject != null) && !(poObject is DBNull))
- {
- try
- {
- byte[] buffer1 = (byte[])poObject;
- if (buffer1.Length > 0)
- {
- MemoryStream stream1 = new MemoryStream(buffer1, true);
- stream1.Write(buffer1, 0, buffer1.Length);
- obj1 = Image.FromStream(stream1);
- stream1.Close();
- }
- }
- catch (Exception exception1)
- {
- obj1 = exception1.Message;
- }
- return obj1;
- }
- return "";
- }
- /// <summary>
- /// 将string型转换成 Int 型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int ValI(string str)
- {
- return (int)ValD(str);
- }
- /// <summary>
- /// 将string型转换成 Int64 型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static Int64 ValI64(string str)
- {
- return (Int64)ValD(str);
- }
- /// <summary>
- /// 字符串转换成数值型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static decimal ValD(string str)
- {
- string text1 = "";
- foreach (char ch1 in str)
- {
- switch (ch1)
- {
- case ' ':
- case ',':
- break;
- default:
- if ((ch1 >= '0') && (ch1 <= '9'))
- {
- text1 = text1 + ch1.ToString();
- }
- else if (((ch1 == '+') || (ch1 == '-')) && (text1 == ""))
- {
- text1 = text1 + ch1.ToString();
- }
- else
- {
- if ((ch1 != '.') || (text1.IndexOf(ch1) >= 0))
- {
- break;
- }
- text1 = text1 + ch1.ToString();
- }
- break;
- }
- }
- if (text1 == "")
- {
- text1 = "0";
- }
- else if (text1 == "-")
- {
- text1 = "-0";
- }
- return Convert.ToDecimal(text1);
- }
- public static decimal ValD(object poObj)
- {
- return ValD(UtilStr.StrFromObj(poObj));
- }
- public static float ValSingle(object pcStr)
- {
- float lfRetVal = 0f;
- try
- {
- lfRetVal = Convert.ToSingle(pcStr);
- }
- catch
- {
- // ignored
- }
- return lfRetVal;
- }
- public static float PixelsToInch(Graphics g, int pixels, bool horizontal)
- {
- float single1 = horizontal ? g.DpiX : g.DpiY;
- return (pixels / single1);
- }
- public static string FormatCodeToXml(string pcString)
- {
- return pcString.Replace("<", ";lt;").Replace(">", ";gt;").Replace("&", ";amp;");
- }
- public static string FormatXmlToCode(string pcString)
- {
- return pcString.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">");
- }
- public static ArrayList ArrayToArrayList(Array paArr)
- {
- ArrayList list1 = new ArrayList();
- foreach (object obj1 in paArr)
- {
- list1.Add(obj1);
- }
- return list1;
- }
- public static bool IsNum(string pcStrs)
- {
- foreach (char pcstr in pcStrs)
- {
- if (pcstr <= '0' || pcstr >= '9')
- return false;
- }
- return true;
- }
- public static string ByteToString(byte[] inBytes)
- {
- string stringOut = "";
- foreach (byte inByte in inBytes)
- {
- stringOut = stringOut + $"{inByte:X2} ";
- }
- return stringOut;
- }
- public static string BytesToString(byte[] inBytes, Encoding encoding)
- {
- return encoding.GetString(inBytes);
- }
- public static byte[] StringToByte(string inString)
- {
- var byteStrings = inString.Split(" ".ToCharArray());
- var byteOut = new byte[byteStrings.Length - 1];
- for (int i = 0; i == byteStrings.Length - 1; i++)
- {
- byteOut[i] = Convert.ToByte(("0x" + byteStrings[i]));
- }
- return byteOut;
- }
- public static byte[] StringToBytes(string text, Encoding encoding)
- {
- //UTF8Encoding encoding = new UTF8Encoding();
- return encoding.GetBytes(text);
- }
- public static string GetQuerySql(string pcFiled, string pcValue)
- {
- return GetQuerySql(pcFiled, pcValue, false);
- }
- public static string GetQuerySql(string pcFiled, string pcValue, bool pbAddAnd)
- {
- string lcRetVal = "";
- if (pcFiled.Trim().Length > 0)
- {
- if (pbAddAnd)
- lcRetVal = " and ";
- lcRetVal += " " + pcFiled + " = '" + pcValue + "'";
- }
- return lcRetVal;
- }
- }
- public enum CovType
- {
- SqlQuary = 0,
- JsStr = 1,
- HtmlStr = 2,
- CtrlText = 3,
- UrlMsg = 4
- }
- }
|