using IWshRuntimeLibrary; using SWRIS.Core; using System; using System.Diagnostics; using System.IO; using System.Net; using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Text; namespace SWRIS.Extensions { public static class Tools { #region 获取 本周、本月、本季度、本年 的开始时间或结束时间 /// /// 获取结束时间 /// /// Week、Month、Season、Year /// /// public static DateTime GetTimeStartByType(string timeType, DateTime time) { switch (timeType) { case "Day": return time.Date; case "Week": DayOfWeek firstDay = DayOfWeek.Monday; // 周一为第一天 return time.AddDays(-((7 + (time.DayOfWeek - firstDay)) % 7)); case "Month": return time.AddDays(-time.Day + 1); case "Season": var time1 = time.AddMonths(0 - ((time.Month - 1) % 3)); return time1.AddDays(-time1.Day + 1); case "Year": return time.AddDays(-time.DayOfYear + 1); default: return time; } } /// /// 获取结束时间 /// /// Week、Month、Season、Year /// /// public static DateTime GetTimeEndByType(string timeType, DateTime time) { switch (timeType) { case "Day": return time.Date.AddDays(1).AddSeconds(-1); case "Week": DayOfWeek firstDay = DayOfWeek.Monday; // 周一为第一天 return time.AddDays(-((7 + (time.DayOfWeek - firstDay)) % 7) + 7).AddSeconds(-1); case "Month": return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddSeconds(-1); case "Season": var time1 = time.AddMonths((3 - ((time.Month - 1) % 3) - 1)); return time1.AddMonths(1).AddDays(-time1.AddMonths(1).Day + 1).AddSeconds(-1); case "Year": var time2 = time.AddYears(1); return time2.AddDays(-time2.DayOfYear).AddSeconds(-1); default: return time; } } #endregion public static string GetWeek(int week) { 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 "星期日"; } } public static string GetIpAddress() { string ip = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { if ("127.0.0.1" == _IPAddress.ToString()) { continue; } else { ip = _IPAddress.ToString(); break; } } } return ip; } /// /// 检查指定的IP地址是否存在于本机的网络接口中 /// /// /// public static bool CheckIpAddressExists(string targetIp) { // 获取所有网络接口 NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { // 只获取运行中的接口 if (ni.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipProperties = ni.GetIPProperties(); // 获取该接口的所有IP地址 foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses) { if (ip.Address.ToString() == targetIp) { return true; } } } } return false; } /// /// 检查指定端口是否被占用 /// /// /// public static bool CheckPortInUse(int port) { IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners(); foreach (IPEndPoint endPoint in endPoints) { if (endPoint.Port == port) { return true; } } return false; } public static byte[] GetMD5Bytes(string input) { using (MD5 md5 = MD5.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); return hashBytes; } } /// /// DateTime 转换为时间戳 /// public static int DateTimeToTimestamp(this DateTime time) { DateTimeOffset dto = new DateTimeOffset(time); return (int)(dto.ToUnixTimeSeconds()); } /// /// 时间戳转换为DateTime /// public static DateTime TimestampToDateTime(this int timestamp) { DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(timestamp); return dto.LocalDateTime; } /// /// 创建桌面会计快捷方式,要求管理员权限运行 /// /// /// /// public static bool CreateShortcut(string applicationPath, string shortcutName) { string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string shortcutPath = Path.Combine(desktopPath, shortcutName + ".lnk"); // 检查快捷方式是否已存在 if (System.IO.File.Exists(shortcutPath)) { return false; // 已存在,不创建 } try { WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); shortcut.TargetPath = applicationPath; shortcut.WorkingDirectory = Path.GetDirectoryName(applicationPath); shortcut.Description = "需要管理员权限运行"; shortcut.IconLocation = applicationPath + ",0"; // 保存快捷方式 shortcut.Save(); // 修改快捷方式以要求管理员权限 using (var fs = new FileStream(shortcutPath, FileMode.Open, FileAccess.ReadWrite)) { fs.Seek(21, SeekOrigin.Begin); fs.WriteByte(0x22); // 设置标志位以管理员身份运行 } return true; // 创建成功 } catch { // 如果创建过程中出错,尝试删除可能已部分创建的快捷方式 if (System.IO.File.Exists(shortcutPath)) { try { System.IO.File.Delete(shortcutPath); } catch { } } throw; } } public static void OpenUrl(string url) { try { Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true // 使用系统默认浏览器 }); } catch (Exception ex) { LogHelper.Error($"打开网页失败:{ex.Message}", ex); } } } }