本文实例讲述了C#实现读取指定盘符硬盘序列号的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
using System; using System.IO; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32; namespace Wjb.ReadOrWriteIniAndReg { /**/ ////// 读取指定盘符的硬盘序列号 /// public class HardDiskVal { [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 ); /**/ /// /// 获得盘符为drvID的硬盘序列号,缺省为C /// /// /// public string HDVal( 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(); } public string HDVal() { const int MAX_FILENAME_LEN = 256; int retVal = 0; int a =0; int b =0; string str1 = null ; string str2 = null ; int i = GetVolumeInformation( "c:\\" , str1, MAX_FILENAME_LEN, ref retVal, a, b, str2, MAX_FILENAME_LEN ); return retVal.ToString(); } } |
希望本文所述对大家C#程序设计有所帮助。