123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System;
- using System.Configuration;
- using System.Drawing;
- using System.Globalization;
- using System.Text.RegularExpressions;
- using System.Threading;
- namespace GCAS.Code
- {
- public class Tools
- {
- public static byte[] StrToToHexByte(string hexString)
- {
- hexString = hexString.Replace(" ", "");
- if ((hexString.Length % 2) != 0)
- hexString += " ";
- byte[] returnBytes = new byte[hexString.Length / 2];
- for (int i = 0; i < returnBytes.Length; i++)
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
- return returnBytes;
- }
- public static int ToIndex(string columnName)
- {
- if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
- int index = 0;
- char[] chars = columnName.ToUpper().ToCharArray();
- for (int i = 0; i < chars.Length; i++)
- {
- index += (chars[i] - 'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
- }
- return index;
- }
- #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
- /// <summary>
- /// 获取结束时间
- /// </summary>
- /// <param name="TimeType">Week、Month、Season、Year</param>
- /// <param name="now"></param>
- /// <returns></returns>
- public static DateTime? GetTimeStartByType(string TimeType, DateTime now)
- {
- switch (TimeType)
- {
- case "Week":
- int dayOfWeek = -1 * (int)now.Date.DayOfWeek;
- DateTime weekStartTime = now.AddDays(dayOfWeek + 1);//取本周一
- if (dayOfWeek == 0) //如果今天是周日,则开始时间是上周一
- {
- weekStartTime = weekStartTime.AddDays(-7);
- }
- return weekStartTime;
- case "Month":
- return now.AddDays(-now.Day + 1);
- case "Season":
- var time = now.AddMonths(0 - ((now.Month - 1) % 3));
- return time.AddDays(-time.Day + 1);
- case "Year":
- return now.AddDays(-now.DayOfYear + 1);
- default:
- return null;
- }
- }
- /// <summary>
- /// 获取结束时间
- /// </summary>
- /// <param name="TimeType">Week、Month、Season、Year</param>
- /// <param name="now"></param>
- /// <returns></returns>
- public static DateTime? GetTimeEndByType(string TimeType, DateTime now)
- {
- switch (TimeType)
- {
- case "Month":
- return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
- case "Season":
- var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
- return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
- case "Year":
- var time2 = now.AddYears(1);
- return time2.AddDays(-time2.DayOfYear);
- default:
- return null;
- }
- }
- #endregion
- public static string GetWeek(int week)
- {
- switch (Thread.CurrentThread.CurrentUICulture.Name)
- {
- case "en":
- return ((DayOfWeek)week).ToString();
- case "zh-Hans":
- switch (week)
- {
- case 1:
- return "星期一";
- case 2:
- return "星期二";
- case 3:
- return "星期三";
- case 4:
- return "星期四";
- case 5:
- return "星期五";
- case 6:
- return "星期六";
- case 7:
- return "星期日";
- default:
- return "未知";
- }
- default:
- return "未知";
- }
- }
- public static string GetWeek(DayOfWeek week)
- {
- switch (Thread.CurrentThread.CurrentUICulture.Name)
- {
- case "en":
- return (week).ToString();
- case "zh-Hans":
- switch (week)
- {
- case DayOfWeek.Sunday:
- return "星期日";
- case DayOfWeek.Monday:
- return "星期一";
- case DayOfWeek.Tuesday:
- return "星期二";
- case DayOfWeek.Wednesday:
- return "星期三";
- case DayOfWeek.Thursday:
- return "星期四";
- case DayOfWeek.Friday:
- return "星期五";
- case DayOfWeek.Saturday:
- return "星期六";
- default:
- return "未知";
- }
- default:
- return "未知";
- }
- }
- public static string GetDay(string time)
- {
- var dateTime = Convert.ToDateTime(DateTime.Now.Year + "-" + time);
- switch (Thread.CurrentThread.CurrentUICulture.Name)
- {
- case "en":
- return dateTime.ToString("MMM. dd", CultureInfo.CreateSpecificCulture("en-GB"));
- case "zh-Hans":
- return dateTime.ToString("MM月dd号");
- default:
- return "未知";
- }
- }
- public static string GetMonth(int month)
- {
- switch (Thread.CurrentThread.CurrentUICulture.Name)
- {
- case "zh-Hans":
- return month.ToString().PadLeft(2, '0') + "月份";
- case "en":
- switch (month)
- {
- case 1:
- return "January";
- case 2:
- return "February";
- case 3:
- return "March";
- case 4:
- return "April";
- case 5:
- return "May";
- case 6:
- return "June";
- case 7:
- return "July";
- case 8:
- return "August";
- case 9:
- return "September";
- case 10:
- return "October";
- case 11:
- return "November";
- case 12:
- return "December";
- default:
- return "未知";
- }
- default:
- return "未知";
- }
- }
- public static Bitmap GetLoginBackground()
- {
- switch (Thread.CurrentThread.CurrentUICulture.Name)
- {
- case "en":
- return Properties.Resources.loginbgEN;
- case "zh-Hans":
- return Properties.Resources.loginbgZH;
- default:
- return Properties.Resources.loginbgZH;
- }
- }
- public static bool ToBool(object obj)
- {
- if (obj == null)
- return false;
- if (obj.ToString().ToUpper() == "TRUE" || obj.ToString() == "1")
- return true;
- if (obj.ToString().ToUpper() == "FALSE" || obj.ToString() == "0")
- return false;
- return false;
- }
- public static void UpdateAppConfig(string newKey, string newValue)
- {
- bool isModified = false;
- foreach (string key in ConfigurationManager.AppSettings)
- {
- if (key == newKey)
- {
- isModified = true;
- }
- }
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (isModified)
- {
- config.AppSettings.Settings.Remove(newKey);
- }
- config.AppSettings.Settings.Add(newKey, newValue);
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- }
- public static T ToEnum<T>(string name)
- {
- Type typeFromHandle = typeof(T);
- if (typeFromHandle.IsEnum)
- {
- return (T)Enum.Parse(typeof(T), name, true);
- }
- throw new InvalidCastException("必须是枚举类型才能转换。");
- }
- }
- }
|