国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Get Volume Serial Number in C#

2024-07-21 02:22:11
字體:
供稿:網(wǎng)友

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);
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武川县| 仁怀市| 莲花县| 乌拉特后旗| 叶城县| 靖宇县| 黑山县| 南郑县| 炉霍县| 宝清县| 曲麻莱县| 汶川县| 喜德县| 陆川县| 德昌县| 兴国县| 平陆县| 博乐市| 自治县| 闸北区| 宝山区| 万宁市| 宁都县| 阳朔县| 金塔县| 南安市| 浪卡子县| 临清市| 江安县| 历史| 油尖旺区| 达日县| 新宾| 湟中县| 南川市| 定襄县| 揭阳市| 玛沁县| 皋兰县| 伊宁县| 蒲城县|