1 using System.Management; 2 3 namespace GLaLa 4 { 5 /// <summary> 6 /// hardware_mac 的摘要说明。 7 /// </summary> 8 class HardwareInfo 9 { 10 /// <summary> 11 /// 取机器名 12 /// </summary> 13 /// <returns></returns> 14 public string GethostName() 15 { 16 return System.Net.Dns.GetHostName(); 17 } 18 19 /// <summary> 20 /// 获取cpu序列号 21 /// </summary> 22 /// <returns></returns> 23 public static string GetCPUSerialNumber() 24 { 25 string cpuSerialNumber = string.Empty; 26 ManagementClass mc = new ManagementClass("Win32_Processor"); 27 ManagementObjectCollection moc = mc.GetInstances(); 28 foreach (ManagementObject mo in moc) 29 { 30 cpuSerialNumber = mo["ProcessorId"].ToString(); 31 break; 32 } 33 mc.Dispose(); 34 moc.Dispose(); 35 return cpuSerialNumber; 36 } 37 38 /// <summary> 39 /// 获取硬盘序列号 40 /// </summary> 41 /// <returns></returns> 42 public static string GetDiskSerialNumber() 43 { 44 ManagementObjectSearcher mos = new ManagementObjectSearcher(); 45 mos.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID", "Signature" }); 46 ManagementObjectCollection myCollection = mos.Get(); 47 ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator(); 48 em.MoveNext(); 49 ManagementBaseObject moo = em.Current; 50 string id = moo.Properties["signature"].Value.ToString().Trim(); 51 return id; 52 } 53 54 /// <summary> 55 /// 获取网卡硬件地址 56 /// </summary> 57 /// <returns></returns> 58 public static string GetMoAddress() 59 { 60 string MoAddress = " "; 61 using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration")) 62 { 63 ManagementObjectCollection moc2 = mc.GetInstances(); 64 foreach (ManagementObject mo in moc2) 65 { 66 //if ((bool)mo["IPEnabled"] == true) 67 MoAddress = mo["MacAddress"].ToString(); 68 mo.Dispose(); 69 } 70 } 71 return MoAddress.ToString(); 72 } 73 } 74 }
调用示例
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
if (File.Exists (Application.StartupPath + @"\WOWComputerInfo.ini") )
{
DecryptoMethod decry = new DecryptoMethod();
string computerInfo = "";
using (StreamReader streamReader = new StreamReader (Application.StartupPath +
@"\WOWComputerInfo.ini", System.Text.Encoding.Default) )
{
try
{
computerInfo = decry.Decrypto (streamReader.ReadToEnd() );
}
catch
{
}
}
string getComputerInfo = HardwareInfo.GetCPUSerialNumber() + HardwareInfo.GetDiskSerialNumber() + HardwareInfo.GetMoAddress();
if (computerInfo != getComputerInfo)
{
MessageBox.Show ("程序不能在本机使用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
else
{
MessageBox.Show ("配置文件丢失或未配置,程序不能运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new frmMain() );
}
============================================================
C#获取系统版本、内存大小、显卡类型、cpu型号
C# Code
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Runtime.InteropServices; private static CPU_INFO CpuInfo; private static MEMORY_INFO MemoryInfo; /// <summary> /// 静态构造函数 /// </summary> static ServerInfo() { CpuInfo = new CPU_INFO(); GetSystemInfo (ref CpuInfo); MemoryInfo = new MEMORY_INFO(); GlobalMemoryStatus (ref MemoryInfo); } #region 服务器相关硬件信息 #region 定义API引用 /// <summary> /// CPU信息 /// </summary> /// <param name="cpuinfo">CPU_INFO</param> public static extern void GetSystemInfo (ref CPU_INFO cpuinfo); /// <summary> public static extern void GlobalMemoryStatus (ref MEMORY_INFO meminfo); public struct CPU_INFO public struct MEMORY_INFO string GetIPAddress() string GetDiskID() /// <summary> |