| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- 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 获取 本周、本月、本季度、本年 的开始时间或结束时间
- /// <summary>
- /// 获取结束时间
- /// </summary>
- /// <param name="timeType">Week、Month、Season、Year</param>
- /// <param name="time"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 获取结束时间
- /// </summary>
- /// <param name="timeType">Week、Month、Season、Year</param>
- /// <param name="time"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 检查指定的IP地址是否存在于本机的网络接口中
- /// </summary>
- /// <param name="targetIp"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 检查指定端口是否被占用
- /// </summary>
- /// <param name="port"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// DateTime 转换为时间戳
- /// </summary>
- public static int DateTimeToTimestamp(this DateTime time)
- {
- DateTimeOffset dto = new DateTimeOffset(time);
- return (int)(dto.ToUnixTimeSeconds());
- }
- /// <summary>
- /// 时间戳转换为DateTime
- /// </summary>
- public static DateTime TimestampToDateTime(this int timestamp)
- {
- DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(timestamp);
- return dto.LocalDateTime;
- }
- /// <summary>
- /// 创建桌面会计快捷方式,要求管理员权限运行
- /// </summary>
- /// <param name="applicationPath"></param>
- /// <param name="shortcutName"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
- }
|