using System;
using System.Runtime.InteropServices;namespace Win
{ public class getvol
{ [DllImport("kernel32.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 = 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");
}
}
}
取C盘的序列号:
string s=Win.getvol.GetVolOf("C");
using System.Runtime.InteropServices;namespace Win
{ public class getvol
{ [DllImport("kernel32.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 = 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");
}
}
}
取C盘的序列号:
string s=Win.getvol.GetVolOf("C");
static extern int GetVolumeInformationA ( string lpRootPathName, string lpVolumeNameBuffer, int nVolumeNameSize, ref int lpVolumeSerialNumber, ref int lpMaximumComponentLength, ref int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize)
用这个api试试.
不过用的是WMI
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].value;
MessageBox.Show(HDid );
}
现添加引用System.management
{
ManagementObject m_objDisk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
//ManagementObject数据管理对象
string strSN = (string)m_objDisk.GetPropertyValue("VolumeSerialNumber");
//获得获取某属性值的等效访问器
MessageBox.Show(strSN);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
(加入日期:2002-1-5 点击数:589)
【对此文发表评论】 【编程爱好者论坛】 【保存文章至硬盘】 【打印文章】
Borland开发讨论区 微软开发讨论区 C/C++讨论区 新手入门专区
在写程序时我们想对每一台计算机都生成一个唯一的标识,而且在一此共享软件中我们也看到,软件在不同的机器上生成了不同的标识,这是如何实现的呢,其实是软件有一部分程序读取了,本地计算机的一部分硬件参数(如硬盘序列号,网卡序列号等等),再通过一系列算法而得到了一个唯一标识,其实我们也可以通过一个api函数生成我们的唯一标识,由于硬盘人人都有,而网卡不一定每个人都有,所以以读硬盘序列号为例,
下面就先介绍一下我们要用到的api函数
BOOL GetVolumeInformation(
LPCTSTR lpRootPathName, // 硬盘的路径
LPTSTR lpVolumeNameBuffer, // 硬盘的卷标
DWORD nVolumeNameSize, // 卷标的字符串长度
LPDWORD lpVolumeSerialNumber, // 硬盘的序列号
LPDWORD lpMaximumComponentLength, // 最大的文件长度
LPDWORD lpFileSystemFlags, // 文件系统的一此标志
LPTSTR lpFileSystemNameBuffer, // 存储所在盘符的分区类型的长指针变量
DWORD nFileSystemNameSize // 分区类型的长指针变量所指向的字符串长度
);
如果上述函数成功就返回一个非0值。光说不做,是不行了,我们还得实践一下:// 最大的文件长度
首选用MFC AppWizard建立一个基于Dialog base的对话框工程,名为GetHardID,点击finish。
加一个按钮,双击它,点击ok,并在对应的函数中加入如下代码:
LPCTSTR lpRootPathName="c:\\"; //取C盘
LPTSTR lpVolumeNameBuffer=new char[12];//磁盘卷标
DWORD nVolumeNameSize=12;// 卷标的字符串长度
DWORD VolumeSerialNumber;//硬盘序列号
DWORD MaximumComponentLength;// 最大的文件长度
LPTSTR lpFileSystemNameBuffer=new char[10];// 存储所在盘符的分区类型的长指针变量
DWORD nFileSystemNameSize=10;// 分区类型的长指针变量所指向的字符串长度
DWORD FileSystemFlags;// 文件系统的一此标志
::GetVolumeInformation(lpRootPathName,
lpVolumeNameBuffer, nVolumeNameSize,
&VolumeSerialNumber, &MaximumComponentLength,
&FileSystemFlags,
lpFileSystemNameBuffer, nFileSystemNameSize);CString str;
str.Format("Seria Num is %lx ",VolumeSerialNumber);
AfxMessageBox(str);编译,链接并运行程序,单击按钮,在弹出的对话框中就是我们要的序列号。
对不对我们验证一下,进入dos窗口,打入"dir c:/p"命令,怎么样是不是和我们的程序显示的一模一样
这样我们就在功告成了。
*********************************************************************
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 );
}
********************************************************************
错误地方:ManagementObjectCollection moc = cimobject.GetInstances();
提示错误:异常详细信息: System.Management.ManagementException: 访问遭到拒绝
http://community.csdn.net/Expert/TopicView1.asp?id=3436398
我的blog上对这个问题作了一些整理,你看看。
http://www.cnblogs.com/zealot/archive/2004/10/08/49942.aspx