C#获取CPU编号

cuifengyin3044 2009-05-06 12:49:29
我想获取CPU编号 方法是这样的:

/// <summary>
/// 获取CPU
/// </summary>
/// <returns></returns>
public string getCPUID()
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
return cpuInfo;
}
为什么在不同电脑上,获取到得CPU编号是一样的。。。CPU应该不是一样的.这个方法是不是不正确。。
这个方法获取到得值是32位的,而我下了一个获取CPU编号的软件获取到得是64位的 谁能帮我解决下。。谢谢了!急!
QQ:280433796 非常感谢
...全文
1628 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnwin 2011-09-11
  • 打赏
  • 举报
回复
取多个硬件的序列号组合一下更好.
henyzhang 2011-09-11
  • 打赏
  • 举报
回复
之前我也需要用到过这种功能,后来网上下载了一段代码解决的。
caschaoxin 2011-09-11
  • 打赏
  • 举报
回复
建议使用硬盘序列号作为唯一标识
零-点 2010-10-30
  • 打赏
  • 举报
回复
学习学习
hanyu0528 2009-05-08
  • 打赏
  • 举报
回复
学习
liuhengwinner 2009-05-08
  • 打赏
  • 举报
回复
为什么在不同电脑上,获取到得CPU编号是一样的?

现很多时候相同的CPU你得到的是相同的型号,CPU ID 现在不像以前那样是唯一的了。 你还是用网卡的 Mac 地址吧。下面是 Mac 地址的代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel.Design;
using Microsoft.Win32;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;


/// <summary>
/// 读取网卡mac地址
/// </summary>


namespace MyUtility
{

public class MacAddr
{
[DllImport("Iphlpapi.dll")]
public static extern uint GetAdaptersAddresses(uint Family, uint flags, IntPtr Reserved,
IntPtr PAdaptersAddresses, ref uint pOutBufLen);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

public class IP_Adapter_Addresses
{
public uint Length;
public uint IfIndex;
public IntPtr Next;

public IntPtr AdapterName;
public IntPtr FirstUnicastAddress;
public IntPtr FirstAnycastAddress;
public IntPtr FirstMulticastAddress;
public IntPtr FirstDnsServerAddress;

public IntPtr DnsSuffix;
public IntPtr Description;

public IntPtr FriendlyName;

[MarshalAs(UnmanagedType.ByValArray,
SizeConst = 8)]
public Byte[] PhysicalAddress;

public uint PhysicalAddressLength;
public uint flags;
public uint Mtu;
public uint IfType;

public uint OperStatus;

public uint Ipv6IfIndex;
public uint ZoneIndices;

public IntPtr FirstPrefix;
}

private string P_NetIP = "";


public MacAddr()
{


}

public ArrayList GetMacAddressList()
{
ArrayList NetAdapterList = new ArrayList();

IntPtr PAdaptersAddresses = new IntPtr();
bool AdapterFound = false;

uint pOutLen = 100;
PAdaptersAddresses = Marshal.AllocHGlobal(100);

uint ret =
GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);

// if 111 error, use
if (ret == 111)
{
Marshal.FreeHGlobal(PAdaptersAddresses);
PAdaptersAddresses = Marshal.AllocHGlobal((int)pOutLen);
ret = GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);
}

IP_Adapter_Addresses adds = new IP_Adapter_Addresses();


IntPtr pTemp = PAdaptersAddresses;
IntPtr pTempIP = new IntPtr();

while (pTemp != (IntPtr)0)
{
Marshal.PtrToStructure(pTemp, adds);
string adapterName = Marshal.PtrToStringAnsi(adds.AdapterName);
string FriendlyName = Marshal.PtrToStringAuto(adds.FriendlyName);
string tmpString = string.Empty;

for (int i = 0; i < 6; i++)
{
tmpString += string.Format("{0:X2}", adds.PhysicalAddress[i]);

if (i < 5)
{
tmpString += ":";
}
}


RegistryKey theLocalMachine = Registry.LocalMachine;

RegistryKey theSystem
= theLocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces");
RegistryKey theInterfaceKey = theSystem.OpenSubKey(adapterName);

if (theInterfaceKey != null)
{

string DhcpIPAddress = (string)theInterfaceKey.GetValue("DhcpIPAddress");

// system is using DHCP
if (DhcpIPAddress != null)
{
string tArray = (string)
theInterfaceKey.GetValue("DhcpIPAddress", theInterfaceKey);

string NetWorkAdapterName = "Network adapter: " + FriendlyName.ToString();
string NetIP = "IP Address: " + tArray ;
string NetMac = "MAC Address: " + tmpString ;
NetAdapterList.Add(NetWorkAdapterName + "<>" + NetIP + "<>" + NetMac);
AdapterFound = true;
}

else
{
string[] tArray = (string[])
theInterfaceKey.GetValue("IPAddress", theInterfaceKey);

string NetWorkAdapterName = "Network adapter: " + FriendlyName.ToString();

P_NetIP = "";
for (int Interface = 0; Interface < tArray.Length; Interface++)
{
P_NetIP += "IP Address: " + tArray[Interface];
AdapterFound = true;
}

string NetMac = "MAC Address: " + tmpString;
NetAdapterList.Add(NetWorkAdapterName + "<>" + P_NetIP + "<>" + NetMac);
}
}

pTemp = adds.Next;
}

if (AdapterFound != true)
{
//AdapterInfoTextBox.Text += "No network adapters configured/present\r\n";
}

return NetAdapterList;
}
}
}

zgke 2009-05-08
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId; //CPU的OEM-ID
public uint dwPageSize; //CPU中的页面大小
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors; //多少个CPU
public uint dwProcessorType; //CPU类型
public uint dwAllocationGranularity;
public uint dwProcessorLevel; //CPU等级
public uint dwProcessorRevision;
}


[DllImport("kernel32.dll")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);


CPU_INFO _CpuInfo = new CPU_INFO();
Win32API.GetSystemInfo(ref _CpuInfo);
_CpuInfo.dwProcessorLevel.ToString();
jdhlowforever 2009-05-08
  • 打赏
  • 举报
回复
新手学习下。
have_love 2009-05-08
  • 打赏
  • 举报
回复
新手来学习下.
qlzf11140820 2009-05-08
  • 打赏
  • 举报
回复
using System.Management;
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["ProcessorId"].Value.ToString();
}
moc = null;
mc = null;
return cpuInfo;
}
catch
{
return "unknow";
}
finally
{
}

}
uncleson88 2009-05-08
  • 打赏
  • 举报
回复
提醒LZ: CPU 是没有唯一序列号的!!!!除了N年前的奔3,
还有硬盘也是,现在大多是 SATA 硬盘,也是没有唯一序列号的,(IDE硬盘有)
所以如果LZ是想确定电脑的唯一性的话 , 最好是打网卡物理地址和主板的主意(但非品牌机的主板好象很多也没有唯一性)
聖少俊 2009-05-07
  • 打赏
  • 举报
回复
学习
zyk113229917 2009-05-07
  • 打赏
  • 举报
回复
http://download.csdn.net/source/1071987

这是完整程序实例
only_lonely 2009-05-07
  • 打赏
  • 举报
回复
余易键1303 2009-05-07
  • 打赏
  • 举报
回复
顶顶顶
zhensoft163 2009-05-06
  • 打赏
  • 举报
回复
给以一段源码,获取cpu号的方法,直接调用

/// <summary>
/// 获得CUP编号
/// </summary>
/// <returns>获得CUP编号</returns>
public String GetCpuID()
{
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();

String strCpuID = null;
foreach (ManagementObject mo in moc)
{
strCpuID = mo.Properties["ProcessorId"].Value.ToString();
break;
}
return strCpuID;
}
catch
{
return "";
}

}


试试看啦,我是可以用的
蔡袅 2009-05-06
  • 打赏
  • 举报
回复
楼主看下面一个博客包你满意:
http://blog.sina.com.cn/s/blog_5b2d74130100b16z.html
cuifengyin3044 2009-05-06
  • 打赏
  • 举报
回复
等待中。。。
mengsijun1987 2009-05-06
  • 打赏
  • 举报
回复
学习
justin麒麟 2009-05-06
  • 打赏
  • 举报
回复
过来学习

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧