Get Volume Serial Number in C#
2008-05-01 04:20
363 查看
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);
}
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);
}
相关文章推荐
- C# 获取硬盘序列号 Volume Serial Number
- [转]C# 获取硬盘序列号 Volume Serial Number
- User Exit to Modify the serial Number in Production Order
- My Web Access Code in C#(Basic Post&Get method) / C#访问Web代码(基本Post和Get方法)
- Serial number is not available when doing any inventory transaction but shows in on hand quantity in
- Volume serial number could associate file existence on certain volume
- SerialPort (RS-232 Serial COM Port) in C# .NET
- [ActionScript 3.0] AS3 获取某年某月的天数(Get number of days in a month)
- Automatically generate serial number in abap
- Code snippet of the day : Get Chinese Spell in C#
- HttpWebRequest GET in C#
- c# API使用GetVolumeInformation函数获取硬盘信息
- Get MSMQ Queue Counts in C#
- Calculate difference between two dates (number of days) in C#?
- Work Tip - Get only date in C#
- Automatically generate serial number in abap
- Get ColumnLetter by ColumnNumber in Excel with VBA
- Get program files folder in C#
- Get nth Max number in an array of unsorted integers.
- USBView & How to get the Serial Number from a USB disk & qextserialport