Get Volume Serial Number in C#

Recently somebody asked how to get the Hard Drive serial number in VB.NET. The easy answer of course is to use VBScript with the WMI classes. Actually, this gets the "Volume" serial number, not the hard-coded manufacturer's hard drive serial number, which needs to be retrieved with custom software that can vary by drive manufacturer. The "Volume" serial number is created when you format a drive, and it can be changed without reformatting, although in practice people rarely do so.

I thought it might be a good experiment to try and do this using the native Windows API "GetVolumeInformation" instead, which requires P/Invoke, in C#. This can be useful information for software and control developers as it can be used to verify licensing for a single computer. For example, on installation we could read the Volume Serial Number of the user's C: drive in our Internet registration module, and have it submit this to a webservice which uses the number as the encryption key for a valid license key to "unlock" the product, which would then be stored in the registry. If somebody attempted to install a single - computer license product on another machine, the key would be invalidated.

The important issue with many of the API's is that you have to get the parameter types correct for them to work with P/Invoke from managed code.

With "GetVolumeInformation", the signature looks as follows:

[DllImport("kernel32.dll")]
private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);

Note that some of the parameters that you would think would be of type "int" must be passed as "UInt32", which corresponds to the .NET type "uint", and oftentimes a string must be passed as StringBuilder.

The actual method , with a string return value for convenience, looks like this:

public string GetVolumeSerial(string strDriveLetter)
{
uint serNum = 0;
uint maxCompLen = 0;
StringBuilder VolLabel = new StringBuilder(256); // Label
UInt32 VolFlags = new UInt32();
StringBuilder FSName = new StringBuilder(256); // File System Name
strDriveLetter+=":\\"; // fix up the passed-in drive letter for the API call
long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);

return Convert.ToString(serNum);
}

时间: 2024-08-03 06:43:16

Get Volume Serial Number in C#的相关文章

MindNode Pro serial number

MindNode Pro serial number 1.6.3 MAC RATED 4.67/5 (93.33%) (6 VOTES) 1 2 3 4 5 Type: Single User SN: KE43VQCQA6SOW-IXS4V3FPVDAZC-5U6ZXIJLWG7SE-7BQ4BAE6UBIVQ Type: Site License SN: YXI7ZKGYVTQ2E-7OELJERSENY5I-SFEVOCBR54U7T-DMHFDPEQIBTQA Type: Educatio

SAP Serial Number在SD中的应用

SAP Serial Number在SD中的应用   SAP中有一个序列号管理的功能,启用这个功能,我们就可以对物料进行精细到「个」地追踪管理.物料主数据为我们提供了一个物料区别于另一个物料的详细信息,而序列号管理则可以让我们实现同一个物料代码下单个物料的精细化管理.   在开始具体的配置及系统应用前,我们先来讨论一下下边两个问题. 1. 序列号(Serial Number)管理和批次(Batch Number)管理有那些不同? 2. 序列号管理可以用在那些业务流程中?   答1:序列号精细到单

SAP Serial Number Management 如何对物料启用序列号管理?

SAP Serial Number Management 如何对物料启用序列号管理?   需要在物料主数据里指派一个serial number profile,在物料主数据的'Work Scheduling' 视图:     2016-12-06 写于武汉市经开区    

InnoDB 中文参考手册 --- 13 出错处理

参考|参考手册|中文 InnoDB 中文参考手册 --- 犬犬(心帆)翻译 13 出错处理InnoDB 的出错处理不总是与 ANSI SQL 指定的一致.依照 ANSI 标准,在一个 SQL 语句中的任何错误都将引起这条语句的回滚.InnoDB 有时只回滚语句的一部分,有时则是整个事务. 下面的列表详细说明了 InnoDB 的出错处理. 如果用完了表空间内的文件空间,将会得到 MySQL 的 'Table is full' 错误,InnoDB 将回滚这条 SQL 语句. 事务的死锁或锁定等待的超

磁盘ID在ASP源码防拷贝中的应用

自从ASP(Active Server Pages)问世以来,因其可以创建健壮易于维护.与平台无关的应用系统,ASP技术受到了越来越多网络程序员的喜爱,使用ASP从事WEB开发的人也越来越多.但ASP只是一种非编译型的.在服务端运行的脚本语言,采用明文(plain text)方式来编写,即使采用了ASP加密程序对ASP源码进行加密,也不一定能保证发布到运行环境中去的ASP应用程序不被非法拷贝.对于高权限的管理员,可以轻而易举从服务器端拷贝出ASP程序应用到别的非授权网站.这样给ASP应用商业化带

asp作品保护方案

自从ASP(Active Server Pages)问世以来,因其可以创建健壮易于维护.与平台无关的应用系统,ASP技术受到了越来越多网络程序员的喜爱,使用ASP从事WEB开发的人也越来越多.但ASP只是一种非编译型的.在服务端运行的脚本语言,采用明文(plain text)方式来编写,即使采用了ASP加密程序对ASP源码进行加密,也不一定能保证发布到运行环境中去的ASP应用程序不被非法拷贝.对于高权限的管理员,可以轻而易举从服务器端拷贝出ASP程序应用到别的非授权网站.这样给ASP应用商业化带

磁盘ID在ASP源码防拷贝中的应用(z)

磁盘ID在ASP源码防拷贝中的应用(孔祥军.苏悦娟 2001年07月26日 17:07) 自从ASP(Active Server Pages)问世以来,因其可以创建健壮易于维护.与平台无关的应用系统,ASP技术受到了越来越多网络程序员的喜爱,使用ASP从事WEB开发的人也越来越多.但ASP只是一种非编译型的.在服务端运行的脚本语言,采用明文(plain text)方式来编写,即使采用了ASP加密程序对ASP源码进行加密,也不一定能保证发布到运行环境中去的ASP应用程序不被非法拷贝.对于高权限的管

DOS的Lable及Date命令使用说明

lable:设置卷标号 [适用场合]用来为磁盘做个标记 [用法]label 显示磁盘卷标 label [盘符] [卷标名] 设定指定盘的卷标 [例子] C:\>label Volume in drive C is WANG Volume Serial Number is 2116-1DD0 volume label (11 characters,Enter for none)? 可以输入卷标,直接回车后 Delete current volume label (Y/N)? 按y删除旧卷标,按n不

汇编源码系列之cdcheck

这个都是过去DOS时代的汇编源码,虽然已经过去了,但是对于学习汇编还是有帮助的,汇编语言只是程序员一门基础语言,大多人掌握即可,不一定要深入研究....... .model small ; It's a flaw of mine ... I really like this model ; I know I should do a .com with the tiny model.. ; but I just love the small :>.stack 100h ; Plenty stack