using System.Management; namespace GLaLa
{
/// <summary>
/// hardware_mac 的摘要说明。
/// </summary>
class HardwareInfo
{
/// <summary>
/// 取机器名
/// </summary>
/// <returns></returns>
public string GethostName()
{
return System.Net.Dns.GetHostName();
} /// <summary>
/// 获取cpu序列号
/// </summary>
/// <returns></returns>
public static string GetCPUSerialNumber()
{
string cpuSerialNumber = string.Empty;
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuSerialNumber = mo["ProcessorId"].ToString();
break;
}
mc.Dispose();
moc.Dispose();
return cpuSerialNumber;
} /// <summary>
/// 获取硬盘序列号
/// </summary>
/// <returns></returns>
public static string GetDiskSerialNumber()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher();
mos.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID", "Signature" });
ManagementObjectCollection myCollection = mos.Get();
ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator();
em.MoveNext();
ManagementBaseObject moo = em.Current;
string id = moo.Properties["signature"].Value.ToString().Trim();
return id;
} /// <summary>
/// 获取网卡硬件地址
/// </summary>
/// <returns></returns>
public static string GetMoAddress()
{
string MoAddress = " ";
using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
//if ((bool)mo["IPEnabled"] == true)
MoAddress = mo["MacAddress"].ToString();
mo.Dispose();
}
}
return MoAddress.ToString();
}
}
}
调用示例
/// <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型号
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> |