[DllImport("kernel32.dll")]
public static extern int GetVolumeInformation(string lpRootPathName,string lpVolumeNameBuffer,int nVolumeNameSize,int lpVolumeSerialNumber,int lpMaximumComponentLength,int lpFileSystemFlags,string lpFileSystemNameBuffer,int nFileSystemNameSize);
public static extern int GetVolumeInformation(string lpRootPathName,string lpVolumeNameBuffer,int nVolumeNameSize,int lpVolumeSerialNumber,int lpMaximumComponentLength,int lpFileSystemFlags,string lpFileSystemNameBuffer,int nFileSystemNameSize);
using System.Management;
.....
ManagementObject m_objDisk = new ManagementObject( "win32_logicaldisk.deviceid=\"c\"");
string strSN = (string)m_objDisk.GetPropertyValue( "VolumeSerialNumber ");如果要取得物理分区号,看这个帖子:
关于硬盘序列号,高手请留步啊. (之一)
http://expert.csdn.net/Expert/TopicView3.asp?id=1143107
ManagementObjectSearcher:用于根据指定的查询或枚举检索 ManagementObject 或 ManagementClass 对象的集合。
ManagementEventWatcher:用于预订来自 WMI 的事件通知。
ManagementQuery:用作所有查询类的基础。 获取硬盘ID
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value; MessageBox.Show(HDid );
}
using System.Runtime.InteropServices;namespace ArLi.CommonPrj {
public class getvol{ [DllImport("kernel32.dll")]
public 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 = 256;
int RetVal = 0;
int a =0;
int b =0;
string str1 = null;
string str2 = null;
int i = GetVolumeInformation(
DrvID + @":\",
str1,
MAX_FILENAME_LEN,
ref RetVal,
a,
b,
str2,
MAX_FILENAME_LEN); return RetVal.ToString("x");
}
}
}
使用方法MessageBox.Show(ArLi.CommonPrj.getvol.getvolOf("C"));够简单吧