using SAGA.DotNetUtils.Extend; namespace SAGA.DotNetUtils { using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Management; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; public class PubMethod : StringOperate { public static string Combine(string strSavePath, string strFileName) { if (strSavePath.IsNullOrEmptyExt()) { throw new ArgumentNullException("strSavePath"); } if (strFileName.IsNullOrEmptyExt()) { throw new ArgumentNullException("strFileName"); } if (!strSavePath.EndsWith(@"\")) { strSavePath = strSavePath + @"\"; } if (strFileName.StartsWith(@"\")) { strFileName = strFileName.TrimStart(new char[] { '\\' }); } return (strSavePath + strFileName); } public static bool CompareBytes(byte[] buffer1, byte[] buffer2) { if ((buffer1 != null) || (buffer2 != null)) { if ((buffer1 == null) || (buffer2 == null)) { return false; } if (buffer1.Length != buffer2.Length) { return false; } for (int i = 0; i < buffer1.Length; i++) { if (buffer1[i] != buffer2[i]) { return false; } } } return true; } public static int CompareDate(DateTime beginDt, DateTime endDt) { if (beginDt.Year > endDt.Year) { return 1; } if (beginDt.Year < endDt.Year) { return -1; } if (beginDt.Month > endDt.Month) { return 1; } if (beginDt.Month < endDt.Month) { return -1; } if (beginDt.Day > endDt.Day) { return 1; } if (beginDt.Day < endDt.Day) { return -1; } return 0; } public static bool CompareFile(string strFile1, string strFile2) { FileInfo info = new FileInfo(strFile1); FileInfo info2 = new FileInfo(strFile2); if (!info.Exists || !info2.Exists) { return false; } if (info.Length != info2.Length) { return false; } try { info.OpenRead(); info2.OpenRead(); } catch (Exception) { return false; } using (FileStream stream = info.OpenRead()) { using (FileStream stream2 = info2.OpenRead()) { for (int i = 0; i < stream.Length; i++) { int num2 = stream.ReadByte(); int num3 = stream2.ReadByte(); if (num2 != num3) { return false; } } } } return true; } public static int CompareTime(DateTime beginDt, DateTime endDt) { int num = CompareDate(beginDt, endDt); if (num != 0) { return num; } if (beginDt.Hour > endDt.Hour) { return 1; } if (beginDt.Hour < endDt.Hour) { return -1; } if (beginDt.Minute > endDt.Minute) { return 1; } if (beginDt.Minute < endDt.Minute) { return -1; } if (beginDt.Second > endDt.Second) { return 1; } if (beginDt.Second < endDt.Second) { return -1; } return 0; } public static string CreateGuidKey() { DateTime now = DateTime.Now; string str = now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0') + now.Millisecond.ToString().PadLeft(3, '0'); string str2 = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpper(); System.Random random = new System.Random(Guid.NewGuid().GetHashCode()); string str3 = random.Next(1, 0x270f).ToString().PadLeft(4, '0'); return (str + "_" + str2 + "_" + str3); } public static string CreateNowTimeString() { DateTime now = DateTime.Now; return (now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0') + now.Millisecond.ToString().PadLeft(3, '0')); } public static string CreateUniqueName() { DateTime now = DateTime.Now; string str = now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0') + now.Millisecond.ToString().PadLeft(3, '0'); System.Random random = new System.Random(Guid.NewGuid().GetHashCode()); string str2 = random.Next(1, 0x270f).ToString().PadLeft(4, '0'); return (str + "_" + str2); } public static bool DeleteFile(string strFileName) { try { if (System.IO.File.Exists(strFileName)) { FileAttribute attribute = new FileAttribute(strFileName) { IsReadOnly = false }; System.IO.File.Delete(strFileName); return true; } return false; } catch { return false; } } public static void DeleteFolder(string folderName) { if (Directory.Exists(folderName)) { DirectoryInfo info = new DirectoryInfo(folderName); foreach (FileInfo info2 in info.GetFiles()) { try { FileAttribute attribute = new FileAttribute(info2.FullName); if (attribute.IsReadOnly) { attribute.IsReadOnly = false; } System.IO.File.Delete(info2.FullName); } catch { } } if (Directory.Exists(folderName)) { foreach (DirectoryInfo info3 in info.GetDirectories()) { DeleteFolder(info3.FullName); } try { Directory.Delete(info.FullName, true); } catch { } } } } public static bool EnumExistsValue(System.Enum source, System.Enum value) { string[] strArray = source.ToString().Trim().Split(new char[] { ',' }); string str2 = value.ToString().Trim(); for (int i = 0; i < strArray.Length; i++) { if (str2 == strArray[i].Trim()) { return true; } } return false; } public static void ExtendLastCol(DataGridView grid) { if (grid.Columns.Count != 0) { int num = 0; foreach (DataGridViewColumn column in grid.Columns) { num += column.Width; } if (grid.Width > num) { DataGridViewColumn column2 = grid.Columns[grid.Columns.Count - 1]; column2.Width = ((grid.Width - num) + column2.Width) - grid.RowHeadersWidth; } } } public static bool FileCopyNew(string strSourceFile, string strDestFile) { bool flag = true; if (!System.IO.File.Exists(strDestFile)) { System.IO.File.Copy(strSourceFile, strDestFile, true); return flag; } FileInfo info = new FileInfo(strSourceFile); FileInfo info2 = new FileInfo(strDestFile); if (info.LastWriteTime > info2.LastWriteTime) { try { FileAttribute attribute = new FileAttribute(strDestFile) { IsReadOnly = false }; System.IO.File.Delete(strDestFile); } catch { flag = false; } try { System.IO.File.Copy(strSourceFile, strDestFile, true); } catch { flag = false; } } return flag; } public static string GetAcadVersion(string strDwgFileName) { if (!System.IO.File.Exists(strDwgFileName)) { return ""; } FileInfo info = new FileInfo(strDwgFileName); if (info.Extension.ToLower() != ".dwg") { return ""; } FileStream stream = new FileStream(strDwgFileName, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[6]; stream.Read(buffer, 0, buffer.Length); string str = new UTF8Encoding(true).GetString(buffer); stream.Close(); return str.Substring(4, 2); } public static string GetCnWeek(string strValue) { string str = string.Empty; switch (strValue.Trim().ToLower()) { case "monday": return "星期一"; case "tuesday": return "星期二"; case "wednesday": return "星期三"; case "thursday": return "星期四"; case "friday": return "星期五"; case "saturday": return "星期六"; case "sunday": return "星期日"; } return str; } public static string GetCpuID() { try { ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances(); string str = null; foreach (ManagementObject obj2 in instances) { str = obj2.Properties["ProcessorId"].Value.ToString().Trim(); if (str.Length > 0) { break; } } return str; } catch { return string.Empty; } } public static DateTime? GetDateTime(object value) { if (value != null) { DateTime time; if (value == DBNull.Value) { return null; } string s = value.ToString().Trim(); if (s.Length <= 0) { return null; } if (DateTime.TryParse(s, out time)) { return new DateTime?(time); } } return null; } public static long GetDriveFreeSpace(string strDrive) { long lpFreeBytesAvalibe = 0L; long lpTotalNumberOfBytes = 0L; long lpTotalNumberOfFreeBytes = 0L; Win32APIMethod.GetDiskFreeSpaceEx(strDrive, out lpFreeBytesAvalibe, out lpTotalNumberOfBytes, out lpTotalNumberOfFreeBytes); return lpFreeBytesAvalibe; } public static string GetDriveFreeSpaceFormat(string strDrive) { double num2 = (((double) GetDriveFreeSpace(strDrive)) / 1024.0) / 1024.0; if (num2 < 1024.0) { return (Math.Round(num2, 2) + " MB"); } return (Math.Round((double) (num2 / 1024.0), 3) + " GB"); } public static int GetDriveType(string driveName) { return Win32APIMethod.GetDriveType(driveName); } public static Icon GetFileIcon(string strFileName, bool blnSmallIcon) { strFileName = Path.GetExtension(strFileName); int num = 0; Icon icon = null; Win32APIMethod.SHFILEINFO psfi = new Win32APIMethod.SHFILEINFO(); if (blnSmallIcon) { num = (int) Win32APIMethod.SHGetFileInfo(strFileName, 0, ref psfi, (uint) Marshal.SizeOf(psfi), 0x111); } else { num = (int) Win32APIMethod.SHGetFileInfo(strFileName, 0, ref psfi, (uint) Marshal.SizeOf(psfi), 0x110); } if ((num > 0) && (((int) psfi.hIcon) != 0)) { icon = Icon.FromHandle(psfi.hIcon); } return icon; } public static bool GetFileThumbnailImage(string strInFile, out string strOutFile) { strOutFile = string.Empty; try { Path.GetExtension(strInFile).ToLower().TrimStart(new char[] { '.' }); if (!true) { return false; } strOutFile = Path.GetDirectoryName(strInFile) + @"\" + Guid.NewGuid().ToString(); int width = 0; int height = 0; int num3 = 800; Image image = null; image = new Bitmap(strInFile); if ((image.Width <= num3) && (image.Height <= num3)) { image.Save(strOutFile, ImageFormat.Jpeg); image.Dispose(); return true; } if (image.Width > image.Height) { if (image.Width > (num3 - 2)) { width = num3 - 2; height = (image.Height * (num3 - 2)) / image.Width; } else { width = image.Width; height = image.Height; } } else if (image.Height > (num3 - 2)) { width = (image.Width * (num3 - 2)) / image.Height; height = num3 - 2; } else { width = image.Width; height = image.Height; } if (width == 0) { width = image.Width; } if (height == 0) { height = image.Height; } Bitmap bitmap = new Bitmap(width, height); try { Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.White); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); graphics.Dispose(); } catch { bitmap = new Bitmap(image.GetThumbnailImage(width, height, null, IntPtr.Zero)); } image.Dispose(); bitmap.Save(strOutFile, ImageFormat.Jpeg); } catch { return false; } return true; } public static string[] GetFontFamilyNames() { FontFamily[] fontFamilys = GetFontFamilys(); string[] strArray = new string[fontFamilys.Length]; for (int i = 0; i < fontFamilys.Length; i++) { strArray[i] = fontFamilys[i].Name; } return strArray; } public static FontFamily[] GetFontFamilys() { return FontFamily.Families; } public static string GetHostAndIp(string IPorHostName, out string HostName) { string str = string.Empty; string str2 = string.Empty; try { IPHostEntry hostEntry = Dns.GetHostEntry(IPorHostName); if (!StringOperate.CheckIP(IPorHostName)) { str2 = IPorHostName; foreach (IPAddress address in hostEntry.AddressList) { if (StringOperate.CheckIP(address.ToString().Trim())) { str = address.ToString().Trim(); goto Label_00C1; } } } else { str = IPorHostName; str2 = hostEntry.HostName.ToLower(); if (str2.IndexOf(".") >= 0) { str2 = str2.Substring(0, str2.IndexOf(".")); } } } catch (SocketException exception) { if (exception.ToString().IndexOf("不知道这样的主机") >= 0) { str = string.Empty; str2 = string.Empty; } } Label_00C1: HostName = str2; return str; } public static int? GetInteger(object value) { if (value != null) { int num; if (value == DBNull.Value) { return null; } string s = value.ToString().Trim(); if (s.Length <= 0) { return null; } if (int.TryParse(s, out num)) { return new int?(num); } } return null; } public static string GetMachineIP() { return GetMachineIP(Environment.MachineName); } public static string GetMachineIP(string MachineName) { string str = string.Empty; if (MachineName != string.Empty) { try { foreach (IPAddress address in Dns.GetHostEntry(MachineName).AddressList) { if (StringOperate.CheckIP(address.ToString().Trim())) { return address.ToString().Trim(); } } } catch { } } return str; } public static string GetNetCardMAC() { try { string str = string.Empty; ManagementClass class2 = new ManagementClass("Win32_NetworkAdapterConfiguration"); foreach (ManagementObject obj2 in class2.GetInstances()) { if ((bool) obj2["IPEnabled"]) { str = obj2["MACAddress"].ToString().Trim(); if (str.Length > 0) { return str; } } } return str; } catch { return string.Empty; } } public static double? GetNumber(object value) { if (value != null) { double num; if (value == DBNull.Value) { return null; } string s = value.ToString().Trim(); if (s.Length <= 0) { return null; } if (double.TryParse(s, out num)) { return new double?(num); } } return null; } public static Image GetThubmnailImage(Image image, int intImageSize) { if (intImageSize == 0) { intImageSize = 200; } int width = 0; int height = 0; if (image.Width > image.Height) { if (image.Width > (intImageSize - 2)) { width = intImageSize - 2; height = (image.Height * (intImageSize - 2)) / image.Width; } else { width = image.Width; height = image.Height; } } else if (image.Height > (intImageSize - 2)) { width = (image.Width * (intImageSize - 2)) / image.Height; height = intImageSize - 2; } else { width = image.Width; height = image.Height; } if (width == 0) { width = image.Width; } if (height == 0) { height = image.Height; } Bitmap bitmap = new Bitmap(intImageSize, intImageSize); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.White); Pen pen = new Pen(Color.Black, 1f); graphics.DrawRectangle(pen, 0, 0, intImageSize - 1, intImageSize - 1); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, new Rectangle((intImageSize - width) / 2, (intImageSize - height) / 2, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); graphics.Dispose(); image.Dispose(); return bitmap; } public static List GroupArray(T[] source, int intGroups) { List list = new List(); if (source.Length < intGroups) { list.Add(source); return list; } int num = source.Length / intGroups; for (int i = 0; i < intGroups; i++) { List list2 = new List(); int num3 = num; if (i == (intGroups - 1)) { num3 += source.Length % intGroups; } for (int j = 0; j < num3; j++) { list2.Add(source[(i * num) + j]); } list.Add(list2.ToArray()); } return list; } public static void KillProcess(string ProName) { try { Process[] processesByName = Process.GetProcessesByName(ProName); if (processesByName.Length > 0) { for (int i = 0; i < processesByName.Length; i++) { processesByName[i].Kill(); Thread.Sleep(0x3e8); } } } catch { } } public static bool KillProcess(string ProName, int intCpuMinutes) { bool flag = false; try { Process[] processesByName = Process.GetProcessesByName(ProName); if (processesByName.Length <= 0) { return flag; } for (int i = 0; i < processesByName.Length; i++) { Process process = processesByName[i]; if (process.TotalProcessorTime.Minutes == intCpuMinutes) { process.Kill(); flag = true; } } } catch { } return flag; } public static bool KillProcess(string ProName, long Memory) { bool flag = false; try { Process[] processesByName = Process.GetProcessesByName(ProName); if (processesByName.Length <= 0) { return flag; } for (int i = 0; i < processesByName.Length; i++) { Process process = processesByName[i]; if (process.WorkingSet64 > Memory) { SystemLogger.Default.Write(" Cur Memory Size : " + process.WorkingSet64.ToString()); process.Kill(); flag = true; } } } catch { } return flag; } public static void NavigateFile(string strFileName) { Process.Start("explorer.exe", "/select," + strFileName); } public static int Random(int intMaxValue) { return new System.Random(Guid.NewGuid().GetHashCode()).Next(-2147483648, intMaxValue); } public static int Random(int intMinValue, int intMaxValue) { return new System.Random(Guid.NewGuid().GetHashCode()).Next(intMinValue, intMaxValue); } public static string RemoveEmptyZero(string s) { if (s == null) { return s; } int index = s.IndexOf("."); if (index <= -1) { return s; } List list = new List(); string str = s.Substring(index); for (int i = 0; i < str.Length; i++) { char c = str[i]; if (char.IsNumber(c)) { string str2 = str.Substring(i); bool flag = false; for (int j = 1; j <= 9; j++) { if (str2.IndexOf(j.ToString()) > -1) { flag = true; break; } } if (flag) { list.Add(c); } continue; } if (c == '.') { string str3 = str.Substring(i); bool flag2 = false; for (int k = 1; k <= 9; k++) { if (str3.IndexOf(k.ToString()) > -1) { flag2 = true; break; } } if (flag2) { list.Add(c); } continue; } list.Add(c); } if (list.Count > 0) { return (s.Substring(0, index) + new string(list.ToArray())); } return s.Substring(0, index); } public static string ReplaceErrChar(object objValue) { return objValue.ToString().Replace(@"\", string.Empty).Replace("'", string.Empty); } public static string ReplaceErrChar(string strValue) { return strValue.Replace(@"\", string.Empty).Replace("'", string.Empty); } public static List SplitArray(T[] source, int intFixLength) { List list = new List(); List list2 = new List(); for (int i = 0; i < source.Length; i++) { list2.Add(source[i]); if ((list2.Count >= intFixLength) || (i == (source.Length - 1))) { list.Add(list2.ToArray()); list2.Clear(); } } return list; } public static void SwapValue(ref T a, ref T b) { T local = a; a = b; b = local; } public static bool TestMachineOnline(string MachineIP) { try { string hostNameOrAddress = MachineIP; if (hostNameOrAddress.IndexOf('\\') > 0) { hostNameOrAddress = hostNameOrAddress.Substring(0, hostNameOrAddress.IndexOf('\\')); } Ping ping = new Ping(); if (ping.Send(hostNameOrAddress, 0x7d0).Status == IPStatus.Success) { return true; } } catch (Exception exception) { SystemLogger.Default.Write("Ping " + MachineIP + exception.ToString()); return true; } SystemLogger.Default.Write("现在不能ping通机器:" + MachineIP); return false; } public static string[] ToStringArray(T[] items) { string[] strArray = new string[items.Length]; for (int i = 0; i < items.Length; i++) { if (items[i] == null) { strArray[i] = string.Empty; } else { strArray[i] = items[i].ToString(); } } return strArray; } } }