123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Management;
- using System.Text;
- using System.Threading.Tasks;
- namespace WebAPIBase.Utils
- {
- public class Computer
- {
- /// <summary>
- /// cpu序列号
- /// </summary>
- public static string CpuID; //1.cpu序列号
- /// <summary>
- /// mac序列号
- /// </summary>
- public static string MacAddress; //2.mac序列号
- /// <summary>
- /// 硬盘id
- /// </summary>
- public static string DiskID; //3.硬盘id
- /// <summary>
- /// .ip地址
- /// </summary>
- public static string IpAddress; //4.ip地址
- /// <summary>
- /// 内存量 单位:M
- /// </summary>
- public static string TotalPhysicalMemory; //8.内存量 单位:M
- /// <summary>
- /// 主板
- /// </summary>
- public static string MotherboardID;
- static Computer()
- {
- CpuID = GetCpuID();
- MacAddress = GetMacAddress();
- DiskID = GetDiskID();
- IpAddress = GetIPAddress();
- MotherboardID = GetMotherboardID();
- TotalPhysicalMemory = GetTotalPhysicalMemory();
- }
- /// <summary>
- /// 获取CPU序列号代码
- /// </summary>
- /// <returns></returns>
- static string GetCpuID()
- {
- try
- {
- string cpuInfo = "";//cpu序列号
- ManagementClass mc = new ManagementClass("Win32_Processor");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
- }
- moc = null;
- mc = null;
- return cpuInfo;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取网卡硬件地址
- /// </summary>
- /// <returns></returns>
- static string GetMacAddress()
- {
- try
- {
- string mac = "";
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if ((bool)mo["IPEnabled"] == true)
- {
- mac = mo["MacAddress"].ToString();
- break;
- }
- }
- moc = null;
- mc = null;
- return mac;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取硬盘ID
- /// </summary>
- /// <returns></returns>
- static string GetDiskID()
- {
- try
- {
- String HDid = "";
- ManagementClass mc = new ManagementClass("Win32_DiskDrive");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- HDid = (string)mo.Properties["Model"].Value;
- }
- moc = null;
- mc = null;
- return HDid;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取主板序列号
- /// </summary>
- /// <returns></returns>
- private static string GetMotherboardID()
- {
- var myMb = new ManagementClass("Win32_BaseBoard").GetInstances();
- var serial = "";
- foreach (ManagementObject mb in myMb)
- {
- var val = mb.Properties["SerialNumber"].Value;
- serial += val == null ? "" : val.ToString();
- }
- return serial;
- }
- /// <summary>
- /// 获取IP地址
- /// </summary>
- /// <returns></returns>
- static string GetIPAddress()
- {
- try
- {
- string st = "";
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if ((bool)mo["IPEnabled"] == true)
- {
- //st=mo["IpAddress"].ToString();
- System.Array ar;
- ar = (System.Array)(mo.Properties["IpAddress"].Value);
- st = ar.GetValue(0).ToString();
- break;
- }
- }
- moc = null;
- mc = null;
- return st;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 操作系统的登录用户名
- /// </summary>
- /// <returns></returns>
- static string GetUserName()
- {
- try
- {
- string un = "";
- un = Environment.UserName;
- return un;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 获取计算机名
- /// </summary>
- /// <returns></returns>
- static string GetComputerName()
- {
- try
- {
- return System.Environment.MachineName;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// PC类型
- /// </summary>
- /// <returns></returns>
- static string GetSystemType()
- {
- try
- {
- string st = "";
- ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- st = mo["SystemType"].ToString();
- }
- moc = null;
- mc = null;
- return st;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 物理内存
- /// </summary>
- /// <returns></returns>
- static string GetTotalPhysicalMemory()
- {
- try
- {
- string st = "";
- ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- st = mo["TotalPhysicalMemory"].ToString();
- }
- moc = null;
- mc = null;
- return st;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- /// <summary>
- /// 根据cpu序列化,主板id及内存信息生成16为授权码(16位HASH代码)
- /// </summary>
- /// <returns></returns>
- public static string AuthorizationCode(string cpu, string motherBoard, string physicalMemory)
- {
- string sensorID = string.Empty;
- string basicID = cpu + motherBoard + physicalMemory;
- System.Security.Cryptography.MD5CryptoServiceProvider mD5CryptoServiceProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] hashedBuff = mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(basicID));
- for (int i = 4; i < 12; i++)
- {
- sensorID += hashedBuff[i].ToString("X2");
- }
- return sensorID;
- }
- }
- }
|