部分电脑出现这个问题!
我在网上找到的大概原因是这个操作磁盘导致的
using System.Management;
是个操作磁盘的类
///<summary> /// 获取硬盘卷标号 ///</summary> ///<returns></returns> public string GetDiskVolumeSerialNumber() { //ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } ///<summary> /// 获取CPU序列号 ///</summary> ///<returns></returns> public string GetCpu() { string strCpu = null; ManagementClass myCpu = new ManagementClass("win32_Processor"); ManagementObjectCollection myCpuCollection = myCpu.GetInstances(); foreach (ManagementObject myObject in myCpuCollection) { strCpu = myObject.Properties["Processorid"].Value.ToString(); } return strCpu; }
好像度娘解决不了这个问题 google 估计只有e文的
谢谢!
用wmi如果客户没有开启这个服务, 你就会报错, 最好的方式还是
[DllImport("Kernel32.dll")] public static extern bool GetVolumeInformation( ref string lpRootPathName, // 系统盘符根目录或网络服务器资源名 ref string lpVolumeNameBuffer, // 磁盘卷标 ref int nVolumeNameSize, // 磁盘卷标字符串的长度 ref int lpVolumeSerialNumber, // 磁盘卷标的序列号 ref int lpMaximumComponentLength, // 文件名长度支持 ref int lpFileSystemFlags, // 文件系统标记 ref string lpFileSystemNameBuffer, // 文件系统类型 ref int nFileSystemNameSize); // 文件系统类型字符串长度
调用:
bool res = GetVolumeInformation(ref strDriver,ref volume,ref vl, refserialNum, ref size, 0,ref type,ref tl);
//取cpu
[DllImport("kernel32")] public static extern void GetSystemInfo(ref CPU_INFO cpuinfo); //定义CPU的信息结构 [StructLayout(LayoutKind.Sequential)] public struct CPU_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; }
//调用
//调用GetSystemInfo函数获取CPU的相关信息
CPU_INFO CpuInfo; CpuInfo = new CPU_INFO(); GetSystemInfo(ref CpuInfo); lstAPI.Items.Add("CPU的OEM ID为"+CpuInfo.dwOemId.ToString());
别用百度, 用google
你这网址有毒啊,赶紧撤掉
@LosPollos_Gus: 已经联系博客园, 现在自己编辑不了了。。 曾经还是个google镜像来的, 现在变成了xx了。 哈哈哈。
是不是相关服务没有开启,
我用下面的方法获取硬盘序列号
http://www.cnblogs.com/xiongpq/p/3953694.html
获取磁盘序列号,最简单的办法是使用fso,里面直接就有一个现成的方法,微软写的,直接返回字符串就行。
在项目的bin目录dll文件。如果文件不是目录,那么它将抛出的错误。所以当复制文件和文件夹确保你复制的所有依赖项(所有引用的类库dll、引用等)。复制“系统”相关的dll文件。
C#获取cpu序列号,硬盘ID,网卡MAC地址
首先在添加引用中选中System.Management
再在开始部门:
using System.Management;
using System.Management.Instrumentation;
private void GetInfo()
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new
ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Response.Write ("cpu序列号:"+cpuInfo.ToString ());
}
//获取硬盘ID
String HDid;
ManagementClass cimobject1 = new
ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach(ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
Response.Write ("硬盘序列号:"+HDid.ToString ());
}
//获取网卡硬件地址
ManagementClass mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach(ManagementObject mo in moc2)
========================================第2页========================================
{
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC
address\t{0}"+mo["MacAddress"].ToString());
mo.Dispose();
}
}
在添加引用中选中
System.Management
再在开始部门:
using System.Management;
using System.Management.Instrumentation;
private void GetInfo()
{
string cpuInfo = "";//cpu
序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Response.Write ("cpu
序列号:
"+cpuInfo.ToString ());
}
//
获取硬盘
ID
String HDid;
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach(ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
Response.Write ("
硬盘序列号:
"+HDid.ToString ());
}
//
获取网卡硬件地址
ManagementClass mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach(ManagementObject mo in moc2)
{
========================================第3页========================================
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC address\t{0}"+mo["MacAddress"].ToString());
mo.Dispose();
}
}
这样的代码 是有问题的 有的电脑是有异常的
这样的代码 是有问题的 有的电脑是有异常的