Disk information
Example statement
In this program, we will generate an application that can get the current disk information, which is similar to the results obtained by right click on the disk "attribute". After running, after selecting the disk, you can get information on the serial number, volume label, file type, used space, unused space, disk capacity.
Technical point
l Judge the drive type
l Take information about the disk
l Take the usage space of the disk, the total space
Implementation process
■ New project
Open Visual Studio.net, select "New Project", select "Visual Basic Project" in the Project Type window, select "Windows Application" in the Template window, enter "DiskInfo" in the Name Domain, and select Save Path. Click "Confirm".
■ Add control
Add sixteen Label controls to the current form, a Picture control, a ComboBox control, a Line control.
■ Setting properties
According to the interface of the form, the TEXT attribute of the illustrative Label control is changed to the interface. Some major properties are listed in Table 96-1, and the remaining attribute readers can refer to the disc.
Table 96-1 Attributes of Forms and Controls
Form / control
Attributes
value
Label10 Text
Backcolor & H000000FF Label11 Text
Value & H00F0000 Other Label Control Autozize True Text is consistent with the interface
■ Add code
The following code is added in the module
Option Strict Off
Module Module1
Public Declare Function GetDriveType lib "kernel32" Alias "getDriveTypea" (Byval NDRIVE AS STRING) AS INTEGER
Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Integer, ByRef lpMaximumComponentLength As Integer, ByRef lpFileSystemFlags As Integer, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As integer) AS Integer
Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, ByRef lpSectorsPerCluster As Integer, ByRef lpBytesPerSector As Integer, ByRef lpNumberOfFreeClusters As Integer, ByRef lpTotalNumberOfClusters As Integer) As Integer
End module
The following code is added in the form
Private sub Combo1_selectedIndexchanged (Byval Eventsender As System.Object, Byval Eventargs As System.EventArgs) Handles Combo1.Selected IndexchangeDdim Buff As String
DIM Retserial, Retroot, Retvolume1, Retflag As String
Dim RetfsnBuffer AS String
DIM LPBYTESPERSECTOR, LPPERCLUSTER, LPFREE AS INTEGER
Dim lptotal as integer
DIM FREESPACE AS Long
DIM TOTALSPACE AS DOUBLE
DIM Usespace as Double
Dim Startang As Single
DIM Endang As Single
DIM X as integer
DIM RETMAXLENGTH AS INTEGER
ON Error ResMe next
BUFF = Combo1.Text & ": /"
'Judging the disk drive type
x = getDriveType (BUFF)
Select Case X
Case 2
LBLDRIVETYPE.TEXT = "Floppy Drive"
Case 3
LBLDRIVETYPE.TEXT = "Hard Drive"
Case 4
LBLDRIVETYPE.TEXT = "Network Drive"
Case 5
LBLDRIVETYPE.TEXT = "CD - ROM Drive"
Case 6
LBLDRIVETYPE.TEXT = "RAMDISK Drive"
Case Else
LBLDriveType.text = ""
End SELECT
'Get the volume name
Retvolume1 = New String (chr (0), 255)
RetfsnBuffer = new string (chr (0), 255)
x = getVolumeInformation (buff, retvolume1, len (retvolume1), retserial, retmaxlength, retflag, retfsnbuffer, len (retfsnbuffer))
'retvolume = retvolume & chr (0)
LBLFILESYSTEM.TEXT = RetfsnBuffer
LBLVOLUME.TEXT = RETVOLUME1
If lblvolume.text = "" ""
LBLVOLUME.TEXT = "No tag"
END IF
'Calculate the remaining space of disk, used space and total space
x = getDiskFreespace (BUFF, LPPPERCLUSTER, LPBYTESPERSECTOR, LPFREE, LPTOTAL)
FreeSpace = lpfree * lpbytespersector * LPPPERCLUSTER
Totalspace = lptotal * lpbytespersector * CDBL (LPPERCLUSTER)
'Totalspace = TotalSpace * 10
LBLunusedSpace.text = cstr (free) & "bytes" & CSTR (int (FreeSpace / 1024/1024)) & "MB" LBLTOTALVOLUME.TEXT = CSTR (TOTALSPACE) & "Byte" & CSTR (Int (Totalspace / 1024 / 1024)) & "MB"
LBLUSPACE.TEXT = CSTR (Totalspace - FreeSpace) & "Bytes" & CSTR (Int (Totalspace - FREESPACE) / 1024/1024)) & "MB"
lblintro.text = "Drive" & Combo1.Text
End Sub
The "Form is started, the disk on the current system is automatically added to the COMBO control.
Private Sub Form1_Load (ByVal Eventsender As System.Object, Byval Eventargs as System.Eventargs) Handles mybase.load
DIM X as integer
DIM I as Short
DIM BUFF AS STRING
For i = 65 to 90
BUFF = CHR (I) & ": /"
x = getDriveType (BUFF)
IF x> 1 THEN
Combo1.Items.Add (chr (i))
END IF
NEXT
Combo1.text = "c"
Call Combo1_Selected IndexChanged (Combo1, New System.EventArgs ())
End Sub
■ Run the program
Click Menu "Debug | Start" or click
Icon running program.
summary
We get information about the disk by using these three getDriveType (), etvolumeinformation (), and ETDiskFreespace () API functions. Among them, the getDriveType () function is used to obtain information about the type of disk drive; the getVolumeInformation () function is used to obtain some information of the disk scrubt; getDiskFreespace () is used to obtain disk remaining space and total spatial information.