从网络上下载一个DiskID32.dll文件,放置到DEBUG目录下.不同电脑获取的编号长度有可能不同.普通台式机长度为8位类似"5VM5GRMT".联想天运F1400位20位类似"080219BB2200WBCZRPZC".
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Security;
using System.Security.Cryptography;
using System.IO;
namespace Common
{
public class ComputerInfo
{
[DllImport("DiskID32.dll")]
public static extern long DiskID32(ref byte DiskModel, ref byte DiskID);
public static string GetDiskID()
{
byte[] DiskModel = new byte[31];
byte[] DiskID = new byte[31];
int i;
string Model = "";
string ID = "";
if (DiskID32(ref DiskModel[0], ref DiskID[0]) != 1)
{
for (i = 0; i < 31; i++)
{
if (Convert.ToChar(DiskID[i]) != Convert.ToChar(0))
{
ID = ID + Convert.ToChar(DiskID[i]);
}
}
ID = ID.Trim();
}
else
{
Console.WriteLine("获取硬盘序列号出错");
}
return ID;
}
}