C# 获取CPU序列号、网卡MAC地址、硬盘序列号封装类,用于软件绑定电脑

 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型号

 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>
/// 内存信息
/// </summary>
/// <param name="meminfo"></param>

public static extern void GlobalMemoryStatus (ref MEMORY_INFO meminfo);
#endregion
#region CPU相关
/// <summary>
/// 获取CPU数量
/// </summary>
public static string Count
{
    get { return CpuInfo.NumberOfProcessors.ToString(); }
}
/// <summary>
/// 获取CPU类型
/// </summary>
public static string Type
{
    get { return CpuInfo.ProcessorType.ToString(); }
}
/// <summary>
/// 获取CPU等级
/// </summary>
public static string Level
{
    get { return CpuInfo.ProcessorLevel.ToString(); }
}
/// <summary>
/// 获取CPUOemID
/// </summary>
public static string OemID
{
    get { return CpuInfo.OemID.ToString(); }
}
/// <summary>
/// CPU页面大小
/// </summary>
public static string PageSize
{
    get { return CpuInfo.PageSize.ToString(); }
}
#endregion
#region 内存相关
/// <summary>
/// 物理内存总大小
/// </summary>
public static string TotalPhys
{
    get { return MemoryInfo.TotalPhys.ToString(); }
}
/// <summary>
/// 可用物理内存
/// </summary>
public static string AvailPhys
{
    get { return MemoryInfo.AvailPhys.ToString(); }
}
/// <summary>
/// 交换页面总大小
/// </summary>
public static string TotalPageFile
{
    get { return MemoryInfo.TotalPageFile.ToString(); }
}
/// <summary>
/// 可交换页面大小
/// </summary>
public static string AvailPageFile
{
    get { return MemoryInfo.AvailPageFile.ToString(); }
}
/// <summary>
/// 虚拟内存总大小
/// </summary>
public static string TotalVirtual
{
    get { return MemoryInfo.TotalVirtual.ToString(); }
}
/// <summary>
/// 可用虚拟内存
/// </summary>
public static string AvailVirtual
{
    get { return MemoryInfo.AvailVirtual.ToString(); }
}
/// <summary>
/// 已经内存
/// </summary>
public static string Load
{
    get { return MemoryInfo.MemoryLoad.ToString(); }
}
#endregion
#endregion
/// <summary>
/// 定义CPU的信息结构
/// </summary>

public struct CPU_INFO
{
    public uint OemID;
    public uint PageSize;
    public uint MinimumApplicationAddress;
    public uint MaximumApplicationAddress;
    public uint ActiveProcessorMask;
    public uint NumberOfProcessors;
    public uint ProcessorType;
    public uint AllocationGranularity;
    public uint ProcessorLevel;
    public uint ProcessorRevision;
}
/// <summary>
/// 定义内存的信息结构
/// </summary>

public struct MEMORY_INFO
{
    public uint Length;
    public uint MemoryLoad;
    public uint TotalPhys;
    public uint AvailPhys;
    public uint TotalPageFile;
    public uint AvailPageFile;
    public uint TotalVirtual;
    public uint AvailVirtual;
}
--------------------------------------------------------
用WMI,在.Net下就是System.Management名称空间,可以获得计算机几乎所有的硬件信息
给你一个类:
很好用的;
using System;
using System.Management;
namespace Soyee.Comm
{
    /// <summary>
    /// Computer Information
    /// </summary>
    public class Computer
    {
        public string CpuID;
        public string MacAddress;
        public string DiskID;
        public string IpAddress;
        public string LoginUserName;
        public string ComputerName;
        public string SystemType;
        public string TotalPhysicalMemory; //单位:M
        private static Computer _instance;
        public static Computer Instance()
        {
            if (_instance == null)
                _instance = new Computer();
            return _instance;
        }
        protected Computer()
        {
            CpuID = GetCpuID();
            MacAddress = GetMacAddress();
            DiskID = GetDiskID();
            IpAddress = GetIPAddress();
            LoginUserName = GetUserName();
            SystemType = GetSystemType();
            TotalPhysicalMemory = GetTotalPhysicalMemory();
            ComputerName = GetComputerName();
        }
        string GetCpuID()
        {
            try
            {
                //获取CPU序列号代码
                string cpuInfo = "";//cpu序列号
                ManagementClass mc = new ManagementClass ("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties.Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
        }
        string GetMacAddress()
        {
            try
            {
                //获取网卡硬件地址
                string mac = "";
                ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ( (bool) mo == true)
                    {
                        mac = mo.ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
        }

string GetIPAddress()
        {
            try
            {
                //获取IP地址
                string st = "";
                ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ( (bool) mo == true)
                    {
                        //st=mo.ToString();
                        System.Array ar;
                        ar = (System.Array) (mo.Properties.Value);
                        st = ar.GetValue (0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }

string GetDiskID()
        {
            try
            {
                //获取硬盘ID
                String HDid = "";
                ManagementClass mc = new ManagementClass ("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string) mo.Properties.Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
        }
        /// <summary>
        /// 操作系统的登录用户名
        /// </summary>
        /// <returns></returns>
        string GetUserName()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass ("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo.ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }

/// <summary>
        /// PC类型
        /// </summary>
        /// <returns></returns>
        string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass ("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo.ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }
        /// <summary>
        /// 物理内存
        /// </summary>
        /// <returns></returns>
        string GetTotalPhysicalMemory()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass ("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo.ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }
        /// <summary>
        /// 获取计算机名
        /// </summary>
        /// <returns></returns>
        string GetComputerName()
        {
            try
            {
                return System.Environment.GetEnvironmentVariable ("ComputerName");
            }
            catch
            {
                return "unknow";
            }
        }
        /// <summary>
        /// 获取用户名机名
        /// </summary>
        /// <returns></returns>
        string GetComputerName()
        {
            try
            {
                return System.Environment.GetEnvironmentVariable ("UserName");
            }
            catch
            {
                return "unknow";
            }
        }
    }
}

上一篇:centos 安装 vsftpd


下一篇:Dynamics CRM Trigger plugin for N:N relationships