Disk Information Instance Description
In this program, we will generate an application that can get the current disk information, similar to the result of right-clicking on the disk "Properties". After running, select the disk, you can get the current disk serial number, volume label, file type, used space, unused space, disk capacity, and other information
Technical points
L Determining Drive Type
L Get information about the disk
L GET the disk usage space, 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 field, and then select Save Path. Click "Confirm".
Adding controls
Adds 16 label controls to the current form, a picture control, a ComboBox control, and a line control.
Setting properties
Depending on the interface of the form, change the Text property of the descriptive label control to be consistent with the interface. Some of the main properties are listed in Table 96-1, and the rest of the properties are available for readers to refer to the CD.
Table 96-1 property values for forms and controls
Forms/Controls
Property
Value
Label10
Text
BackColor
&h000000ff
Label11
Text
Value
&h00ff0000
Other label controls
Autosize
True
Text
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 Lpmax Imumcomponentlength As Integer, ByRef lpfilesystemflags As Integer, ByVal lpfilesystemnamebuffer as String, ByVal Nfilesys Temnamesize as Integer) As Integer
Public Declare Function getdiskfreespace Lib "kernel32" Alias "Getdiskfreespacea" (ByVal lprootpathname as String, ByRef l Psectorspercluster As Integer, ByRef lpbytespersector As Integer, ByRef lpnumberoffreeclusters As Integer, ByRef lptotalnu Mberofclusters as Integer) As Integer
End Module
The following code is added to the form
Private Sub combo1_selectedindexchanged (ByVal Eventsender as System.Object, ByVal EventArgs as System.EventArgs) Handles Combo1.selectedindexchanged
Dim 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 Resume Next
Buff = Combo1.text & ":"
' Determine the type of disk drive
x = GetDriveType (Buff)
Select Case X
Case 2
Lbldrivetype. Text = "Floppy disk 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 label 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 = "" Then
Lblvolume. Text = "No volume label"
End If
' Compute the disk remaining space, used space, and total space
x = GetDiskFreeSpace (Buff, Lppercluster, Lpbytespersector, Lpfree, Lptotal)
FreeSpace = Lpfree * Lpbytespersector * lppercluster
Totalspace = lptotal * Lpbytespersector * CDBL (Lppercluster)
' Totalspace = totalspace * 10
Lblunusedspace.text = CStr (freespace) & "bytes" & CStr (Int (freespace/1024/1024)) & "MB"
Lbltotalvolume.text = CStr (totalspace) & "bytes" & CStr (Int (totalspace/1024/1024)) & "MB"
Lblusedspace.text = CStr (totalspace-freespace) & "bytes" & CStr (Int (totalspace-freespace)/1024/1024) ; "MB"
Lblintro. Text = "Drive" & Combo1.text
End Sub
' Automatically add disks from the current system to the combo control when the form starts
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 = 90
Buff = CHR (i) & ":"
x = GetDriveType (Buff)
If x > 1 Then
COMBO1.ITEMS.ADD (CHR (i))
End If
Next
Combo1.text = "C"
Call Combo1_selectedindexchanged (Combo1, New System.EventArgs ())
End Sub
Running programs
Click the Menu Debug | Start or click the icon to run the program.
Summary
We get information about the disk by using these three getdrivetype (), Etvolumeinformation (), Etdiskfreespace () API functions. where the GetDriveType () function is used to obtain information about the disk drive type, the GetVolumeInformation () function is used to obtain some information about the disk volume label, and GetDiskFreeSpace () is used to obtain the disk remaining space and the total space information.