using System; using System.Collections.Generic; using System.Text; using NetLibrary.Safety; using NetLibrary.Common.Configuration; using System.Management; using System.Runtime.InteropServices; using System.Drawing; using System.Diagnostics; namespace NetLibrary { public static class SystemInfo { [DllImport("winmm.dll", EntryPoint = "PlaySound", SetLastError = true)] public static extern bool W32_PlaySound(string szSound, IntPtr hmod, UInt32 fdwSound); [DllImport("winmm.dll", EntryPoint = "PlaySound", SetLastError = true)] public static extern bool W32_PlaySound(byte[] szSound, IntPtr hmod, UInt32 fdwSound); [DllImport("user32")] public static extern long ExitWindowsEx(long uFlags, long dwReserved); //定义API变量 [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop); [DllImport("Kernel32.dll", SetLastError = true)] public static extern int SetLocalTime(ref SystemTime lpSystemTime); public struct SystemTime { public short wYear; public short wMonth; public short wDayOfWeek; public short wDay; public short wHour; public short wMinute; public short wSecond; public short wMilliseconds; } public class SoundFlag { public const UInt32 SND_SYNC = 0x0000; // 同步 public const UInt32 SND_ASYNC = 0x0001; // 用异步方式播放声音,PlaySound函数在开始播放后立即返回。 public const UInt32 SND_NODEFAULT = 0x0002; // 静音 public const UInt32 SND_MEMORY = 0x0004; // 流文件 public const UInt32 SND_LOOP = 0x0008; // 循环 public const UInt32 SND_NOSTOP = 0x0010; // 不打断原来的声音播出并立即返回FALSE。 public const UInt32 SND_NOWAIT = 0x00002000; // 如果驱动程序正忙则函数就不播放声音并立即返回。 public const UInt32 SND_ALIAS = 0x00010000; // name is a registry alias public const UInt32 SND_ALIAS_ID = 0x00110000; // alias is a predefined ID public const UInt32 SND_FILENAME = 0x00020000; // name is file name public const UInt32 SND_RESOURCE = 0x00040004; // name is resource name or atom } #region 获取CPU序列号 /// /// 获取CPU序列号 /// public static string GetProcessorID() { try { string cpuInfo = "";//cpu序列号 ManagementClass cimobject = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); if (cpuInfo != "") { return cpuInfo; } } } catch { return ""; } return ""; } #endregion #region 获取网卡硬件地址 /// /// 获取网卡硬件地址 /// public static string GetNetworkCardID() { try { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) return mo["MacAddress"].ToString(); mo.Dispose(); } } catch { return ""; } return ""; } #endregion #region 获取硬盘ID /// /// 获取硬盘ID /// public static string GetDiskDriveID() { try { String HDid; ManagementClass cimobject = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach (ManagementObject mo in moc) { HDid = (string)mo.Properties["Model"].Value; return HDid; } } catch { return ""; } return ""; } #endregion #region 判断注册码是否正确 /// /// 判断注册码是否正确 /// public static bool IsRegisterCode(string filePath, string pubkey, string keyName) { CryptoPublic cp = new CryptoPublic(); //string pubkey = "0zzXz1C4sKN0bWqWhMs8fp8EG+xI1o5f2xe/HztS65PPp2DGMpOfY9hyOuFW9Rkfm/aBeenzmBaqRhG0h5spc/0SRSbEWR76oP/3z/mDmWBBqsbz7iKDuEs08Zi34UogmRX2t5FpQmcVfjBNvWhYhEoDUDxutRvmNHB4P0/nluE=AQAB"; string jqm = ""; //返回CPU序列号 jqm = SystemInfo.GetProcessorID(); if (jqm == "") { jqm = SystemInfo.GetNetworkCardID(); } if (jqm == "") { jqm = SystemInfo.GetDiskDriveID(); } string zcm = ConfigurationSourceSection.LoadXml_Attribute(filePath, keyName); //返回注册码 if (cp.RsaVerifySignature(jqm, zcm, pubkey) == false) { return false; } return true; } #endregion #region 设置系统时间 public static void SetDateTime(DateTime dt) { SystemTime st = new SystemTime(); st.wYear = Convert.ToInt16(dt.Year); st.wMonth = Convert.ToInt16(dt.Month); st.wDay = Convert.ToInt16(dt.Day); st.wHour = Convert.ToInt16(dt.Hour); st.wMinute = Convert.ToInt16(dt.Minute); st.wSecond = Convert.ToInt16(dt.Second); st.wMilliseconds = Convert.ToInt16(dt.Millisecond); st.wDayOfWeek = Convert.ToInt16(dt.DayOfWeek); SetLocalTime(ref st); } #endregion #region 根据颜色名称创建颜色 public static Color CreateColor(string ColorName) { switch (ColorName) { case "AliceBlue": return Color.AliceBlue; case "AntiqueWhite": return Color.AntiqueWhite; case "Aqua": return Color.Aqua; case "Aquamarine": return Color.Aquamarine; case "Azure": return Color.Azure; case "Beige": return Color.Beige; case "Bisque": return Color.Bisque; case "Black": return Color.Black; case "BlanchedAlmond": return Color.BlanchedAlmond; case "Blue": return Color.Blue; case "BlueViolet": return Color.BlueViolet; case "Brown": return Color.Brown; case "BurlyWood": return Color.BurlyWood; case "CadetBlue": return Color.CadetBlue; case "Chartreuse": return Color.Chartreuse; case "Chocolate": return Color.Chocolate; case "Coral": return Color.Coral; case "CornflowerBlue": return Color.CornflowerBlue; case "Cornsilk": return Color.Cornsilk; case "Crimson": return Color.Crimson; case "Cyan": return Color.Cyan; case "DarkBlue": return Color.DarkBlue; case "DarkCyan": return Color.DarkCyan; case "DarkGoldenrod": return Color.DarkGoldenrod; case "DarkGray": return Color.DarkGray; case "DarkGreen": return Color.DarkGreen; case "DarkKhaki": return Color.DarkKhaki; case "DarkMagenta": return Color.DarkMagenta; case "DarkOliveGreen": return Color.DarkOliveGreen; case "DarkOrange": return Color.DarkOrange; case "DarkOrchid": return Color.DarkOrchid; case "DarkRed": return Color.DarkRed; case "DarkSalmon": return Color.DarkSalmon; case "DarkSeaGreen": return Color.DarkSeaGreen; case "DarkSlateBlue": return Color.DarkSlateBlue; case "DarkSlateGray": return Color.DarkSlateGray; case "DarkTurquoise": return Color.DarkTurquoise; case "DarkViolet": return Color.DarkViolet; case "DeepPink": return Color.DeepPink; case "DeepSkyBlue": return Color.DeepSkyBlue; case "DimGray": return Color.DimGray; case "DodgerBlue": return Color.DodgerBlue; case "Firebrick": return Color.Firebrick; case "FloralWhite": return Color.FloralWhite; case "ForestGreen": return Color.ForestGreen; case "Fuchsia": return Color.Fuchsia; case "Gainsboro": return Color.Gainsboro; case "GhostWhite": return Color.GhostWhite; case "Gold": return Color.Gold; case "Goldenrod": return Color.Goldenrod; case "Gray": return Color.Gray; case "Green": return Color.Green; case "GreenYellow": return Color.GreenYellow; case "Honeydew": return Color.Honeydew; case "HotPink": return Color.HotPink; case "IndianRed": return Color.IndianRed; case "Indigo": return Color.Indigo; case "Ivory": return Color.Ivory; case "Khaki": return Color.Khaki; case "Lavender": return Color.Lavender; case "LavenderBlush": return Color.LavenderBlush; case "LawnGreen": return Color.LawnGreen; case "LemonChiffon": return Color.LemonChiffon; case "LightBlue": return Color.LightBlue; case "LightCoral": return Color.LightCoral; case "LightCyan": return Color.LightCyan; case "LightGoldenrodYellow": return Color.LightGoldenrodYellow; case "LightGray": return Color.LightGray; case "LightGreen": return Color.LightGreen; case "LightPink": return Color.LightPink; case "LightSalmon": return Color.LightSalmon; case "LightSeaGreen": return Color.LightSeaGreen; case "LightSkyBlue": return Color.LightSkyBlue; case "LightSlateGray": return Color.LightSlateGray; case "LightSteelBlue": return Color.LightSteelBlue; case "LightYellow": return Color.LightYellow; case "Lime": return Color.Lime; case "LimeGreen": return Color.LimeGreen; case "Linen": return Color.Linen; case "Magenta": return Color.Magenta; case "Maroon": return Color.Maroon; case "MediumAquamarine": return Color.MediumAquamarine; case "MediumBlue": return Color.MediumBlue; case "MediumOrchid": return Color.MediumOrchid; case "MediumPurple": return Color.MediumPurple; case "MediumSeaGreen": return Color.MediumSeaGreen; case "MediumSlateBlue": return Color.MediumSlateBlue; case "MediumSpringGreen": return Color.MediumSpringGreen; case "MediumTurquoise": return Color.MediumTurquoise; case "MediumVioletRed": return Color.MediumVioletRed; case "MidnightBlue": return Color.MidnightBlue; case "MintCream": return Color.MintCream; case "MistyRose": return Color.MistyRose; case "Moccasin": return Color.Moccasin; case "NavajoWhite": return Color.NavajoWhite; case "Navy": return Color.Navy; case "OldLace": return Color.OldLace; case "Olive": return Color.Olive; case "OliveDrab": return Color.OliveDrab; case "Orange": return Color.Orange; case "OrangeRed": return Color.OrangeRed; case "Orchid": return Color.Orchid; case "PaleGoldenrod": return Color.PaleGoldenrod; case "PaleGreen": return Color.PaleGreen; case "PaleTurquoise": return Color.PaleTurquoise; case "PaleVioletRed": return Color.PaleVioletRed; case "PapayaWhip": return Color.PapayaWhip; case "PeachPuff": return Color.PeachPuff; case "Peru": return Color.Peru; case "Pink": return Color.Pink; case "Plum": return Color.Plum; case "PowderBlue": return Color.PowderBlue; case "Purple": return Color.Purple; case "Red": return Color.Red; case "RosyBrown": return Color.RosyBrown; case "SaddleBrown": return Color.SaddleBrown; case "Salmon": return Color.Salmon; case "SandyBrown": return Color.SandyBrown; case "SeaGreen": return Color.SeaGreen; case "SeaShell": return Color.SeaShell; case "Sienna": return Color.Sienna; case "Silver": return Color.Silver; case "SkyBlue": return Color.SkyBlue; case "SlateBlue": return Color.SlateBlue; case "SlateGray": return Color.SlateGray; case "Snow": return Color.Snow; case "SpringGreen": return Color.SpringGreen; case "SteelBlue": return Color.SteelBlue; case "Tan": return Color.Tan; case "Teal": return Color.Teal; case "Thistle": return Color.Thistle; case "Tomato": return Color.Tomato; case "Transparent": return Color.Transparent; case "Turquoise": return Color.Turquoise; case "Violet": return Color.Violet; case "Wheat": return Color.Wheat; case "White": return Color.White; case "WhiteSmoke": return Color.WhiteSmoke; case "Yellow": return Color.Yellow; case "YellowGreen": return Color.YellowGreen; default: return Color.Black; } } #endregion #region 像素转毫米 public static int PxConvertMm(int Pixel) { ManagementClass mc = new ManagementClass("Win32_DesktopMonitor"); ManagementObjectCollection moc = mc.GetInstances(); int PixelsPerXLogicalInch = 0; // dpi for x //int PixelsPerYLogicalInch = 0; // dpi for y foreach (ManagementObject each in moc) { PixelsPerXLogicalInch = int.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString())); //PixelsPerYLogicalInch = int.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString())); } int a =Convert.ToInt32(Pixel / PixelsPerXLogicalInch * 25.4); return a; } #endregion #region 读取当前文件夹路径 public static string AppPath() { string path = AppDomain.CurrentDomain.BaseDirectory; path = path.Replace("\\", "/"); return path; } #endregion #region 播放声音文件 public static void PlayWav(string FileName) { try { W32_PlaySound(FileName, IntPtr.Zero, SoundFlag.SND_ASYNC + SoundFlag.SND_FILENAME); } catch { } } #endregion #region 关机 public static void CloseWindows() { Process MyProcess = new Process(); //设定程序名 MyProcess.StartInfo.FileName = "cmd.exe"; //关闭Shell的使用 MyProcess.StartInfo.UseShellExecute = false; //重定向标准输入 MyProcess.StartInfo.RedirectStandardInput = true; //重定向标准输出 MyProcess.StartInfo.RedirectStandardOutput = true; //重定向错误输出 MyProcess.StartInfo.RedirectStandardError = true; //设置不显示窗口 MyProcess.StartInfo.CreateNoWindow = true; //执行VER命令 MyProcess.Start(); MyProcess.StandardInput.WriteLine("shutdown -f -s -t 1"); MyProcess.StandardInput.WriteLine("exit"); } #endregion } }