Get Volume serial number in C #

Source: Internet
Author: User
Tags file system
Recently somebody asked the Hard Drive serial number in vb.net. The easy answer of course was to use VBScript with the WMI classes. Actually, this is gets the "Volume" serial number, not the hard-coded manufacturer ' s hard drive-serial number, which needs to Be retrieved with custom software, can vary by drive manufacturer. The "Volume" serial number is created to format a drive, and it can be changed without, reformatting in PRA Ctice people rarely.

I thought it might be a good experiment to try and do this using the native Windows API "GetVolumeInformation" instead, WH Ich requires P/invoke, in C #. This can is useful information for software and control developers as it can is used to verify licensing for a single comp Uter. For example, on installation we could read the Volume serial number of the the user's c:drive in our Internet registration mo Dule, and have it submit this to a webservice which uses the number as the encryption key for a valid license key to "Unlo CK "The product, which would then is stored in the registry. If Somebody attempted to install a Single-computer license product on another machine, the key would is invalidated.

The important issue with many of the API ' s are ' you have to get the parameter types correct for them to work with P/INV Oke 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 ' you would to would be of type ' int ' must be passed as "UInt32", which corresponds to the. NET type "uint", and oftentimes a string must is 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 the ' API call
Long Ret = GetVolumeInformation (Strdriveletter, Vollabel, (UInt32) vollabel.capacity, ref sernum, ref Maxcomplen, ref VolF Lags, FSName, (UInt32) fsname.capacity);

Return convert.tostring (Sernum);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.