Tools.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Configuration;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. namespace GCAS.Code
  8. {
  9. public class Tools
  10. {
  11. public static byte[] StrToToHexByte(string hexString)
  12. {
  13. hexString = hexString.Replace(" ", "");
  14. if ((hexString.Length % 2) != 0)
  15. hexString += " ";
  16. byte[] returnBytes = new byte[hexString.Length / 2];
  17. for (int i = 0; i < returnBytes.Length; i++)
  18. returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  19. return returnBytes;
  20. }
  21. public static int ToIndex(string columnName)
  22. {
  23. if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
  24. int index = 0;
  25. char[] chars = columnName.ToUpper().ToCharArray();
  26. for (int i = 0; i < chars.Length; i++)
  27. {
  28. index += (chars[i] - 'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
  29. }
  30. return index;
  31. }
  32. #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
  33. /// <summary>
  34. /// 获取结束时间
  35. /// </summary>
  36. /// <param name="TimeType">Week、Month、Season、Year</param>
  37. /// <param name="now"></param>
  38. /// <returns></returns>
  39. public static DateTime? GetTimeStartByType(string TimeType, DateTime now)
  40. {
  41. switch (TimeType)
  42. {
  43. case "Week":
  44. int dayOfWeek = -1 * (int)now.Date.DayOfWeek;
  45. DateTime weekStartTime = now.AddDays(dayOfWeek + 1);//取本周一
  46. if (dayOfWeek == 0) //如果今天是周日,则开始时间是上周一
  47. {
  48. weekStartTime = weekStartTime.AddDays(-7);
  49. }
  50. return weekStartTime;
  51. case "Month":
  52. return now.AddDays(-now.Day + 1);
  53. case "Season":
  54. var time = now.AddMonths(0 - ((now.Month - 1) % 3));
  55. return time.AddDays(-time.Day + 1);
  56. case "Year":
  57. return now.AddDays(-now.DayOfYear + 1);
  58. default:
  59. return null;
  60. }
  61. }
  62. /// <summary>
  63. /// 获取结束时间
  64. /// </summary>
  65. /// <param name="TimeType">Week、Month、Season、Year</param>
  66. /// <param name="now"></param>
  67. /// <returns></returns>
  68. public static DateTime? GetTimeEndByType(string TimeType, DateTime now)
  69. {
  70. switch (TimeType)
  71. {
  72. case "Month":
  73. return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
  74. case "Season":
  75. var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
  76. return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
  77. case "Year":
  78. var time2 = now.AddYears(1);
  79. return time2.AddDays(-time2.DayOfYear);
  80. default:
  81. return null;
  82. }
  83. }
  84. #endregion
  85. public static string GetWeek(int week)
  86. {
  87. switch (Thread.CurrentThread.CurrentUICulture.Name)
  88. {
  89. case "en":
  90. return ((DayOfWeek)week).ToString();
  91. case "zh-Hans":
  92. switch (week)
  93. {
  94. case 1:
  95. return "星期一";
  96. case 2:
  97. return "星期二";
  98. case 3:
  99. return "星期三";
  100. case 4:
  101. return "星期四";
  102. case 5:
  103. return "星期五";
  104. case 6:
  105. return "星期六";
  106. case 7:
  107. return "星期日";
  108. default:
  109. return "未知";
  110. }
  111. default:
  112. return "未知";
  113. }
  114. }
  115. public static string GetWeek(DayOfWeek week)
  116. {
  117. switch (Thread.CurrentThread.CurrentUICulture.Name)
  118. {
  119. case "en":
  120. return (week).ToString();
  121. case "zh-Hans":
  122. switch (week)
  123. {
  124. case DayOfWeek.Sunday:
  125. return "星期日";
  126. case DayOfWeek.Monday:
  127. return "星期一";
  128. case DayOfWeek.Tuesday:
  129. return "星期二";
  130. case DayOfWeek.Wednesday:
  131. return "星期三";
  132. case DayOfWeek.Thursday:
  133. return "星期四";
  134. case DayOfWeek.Friday:
  135. return "星期五";
  136. case DayOfWeek.Saturday:
  137. return "星期六";
  138. default:
  139. return "未知";
  140. }
  141. default:
  142. return "未知";
  143. }
  144. }
  145. public static string GetDay(string time)
  146. {
  147. var dateTime = Convert.ToDateTime(DateTime.Now.Year + "-" + time);
  148. switch (Thread.CurrentThread.CurrentUICulture.Name)
  149. {
  150. case "en":
  151. return dateTime.ToString("MMM. dd", CultureInfo.CreateSpecificCulture("en-GB"));
  152. case "zh-Hans":
  153. return dateTime.ToString("MM月dd号");
  154. default:
  155. return "未知";
  156. }
  157. }
  158. public static string GetMonth(int month)
  159. {
  160. switch (Thread.CurrentThread.CurrentUICulture.Name)
  161. {
  162. case "zh-Hans":
  163. return month.ToString().PadLeft(2, '0') + "月份";
  164. case "en":
  165. switch (month)
  166. {
  167. case 1:
  168. return "January";
  169. case 2:
  170. return "February";
  171. case 3:
  172. return "March";
  173. case 4:
  174. return "April";
  175. case 5:
  176. return "May";
  177. case 6:
  178. return "June";
  179. case 7:
  180. return "July";
  181. case 8:
  182. return "August";
  183. case 9:
  184. return "September";
  185. case 10:
  186. return "October";
  187. case 11:
  188. return "November";
  189. case 12:
  190. return "December";
  191. default:
  192. return "未知";
  193. }
  194. default:
  195. return "未知";
  196. }
  197. }
  198. public static Bitmap GetLoginBackground()
  199. {
  200. switch (Thread.CurrentThread.CurrentUICulture.Name)
  201. {
  202. case "en":
  203. return Properties.Resources.loginbgEN;
  204. case "zh-Hans":
  205. return Properties.Resources.loginbgZH;
  206. default:
  207. return Properties.Resources.loginbgZH;
  208. }
  209. }
  210. public static bool ToBool(object obj)
  211. {
  212. if (obj == null)
  213. return false;
  214. if (obj.ToString().ToUpper() == "TRUE" || obj.ToString() == "1")
  215. return true;
  216. if (obj.ToString().ToUpper() == "FALSE" || obj.ToString() == "0")
  217. return false;
  218. return false;
  219. }
  220. public static void UpdateAppConfig(string newKey, string newValue)
  221. {
  222. bool isModified = false;
  223. foreach (string key in ConfigurationManager.AppSettings)
  224. {
  225. if (key == newKey)
  226. {
  227. isModified = true;
  228. }
  229. }
  230. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  231. if (isModified)
  232. {
  233. config.AppSettings.Settings.Remove(newKey);
  234. }
  235. config.AppSettings.Settings.Add(newKey, newValue);
  236. config.Save(ConfigurationSaveMode.Modified);
  237. ConfigurationManager.RefreshSection("appSettings");
  238. }
  239. public static T ToEnum<T>(string name)
  240. {
  241. Type typeFromHandle = typeof(T);
  242. if (typeFromHandle.IsEnum)
  243. {
  244. return (T)Enum.Parse(typeof(T), name, true);
  245. }
  246. throw new InvalidCastException("必须是枚举类型才能转换。");
  247. }
  248. }
  249. }