日期:2014-05-19  浏览次数:20991 次

c#读取硬盘物理序列号 在线等待........
问题如下:
c#读取硬盘物理序列号

请大家帮帮忙

谢谢!

------解决方案--------------------
using System;
using System.Runtime.InteropServices;

namespace ArLi.CommonPrj {

#region how use this?
/*
string sVol = getvol.GetVolOf( "C ");
*/
#endregion

public class getvol{

[DllImport( "kernel.dll ")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);

public static string GetVolOf(string drvID){
const int MAX_FILENAME_LEN = 6;
int retVal = 0;
int a =0;
int b =0;
string str = null;
string str = null;


int i = GetVolumeInformation(
drvID + @ ":\ ",
str,
MAX_FILENAME_LEN,
ref retVal,
a,
b,
str,
MAX_FILENAME_LEN
);

return retVal.ToString( "x ");
}
}
}

------解决方案--------------------
发帖前搜索一下是一个优良的习惯
http://topic.csdn.net/t/20060529/11/4785648.html
------解决方案--------------------
用 DeviceIoControl

wmi 有权限限制
------解决方案--------------------
试试这个吧

using System;
using System.Management;

namespace HTSoft.Common.Register
{
/// <summary>
/// 计算机信息类
/// </summary>
internal 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;

internal static Computer Instance()
{
if (_instance == null)
_instance = new Computer();
return _instance;
}

internal 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[ "ProcessorId "].Value.ToString();
}
moc = null;
mc = null;
return cpuInfo;
}
catch
{
return "unknow ";
}
finally
{
}

}
string GetMacAddress()