VB. NET program example for obtaining disk Information

Source: Internet
Author: User

Next let's take a look at VB. program example for obtaining disk information. In this example, three functions are used: GetDriveType (), etVolumeInformation (), etDiskFreeSpace (), let's take a look at how Alibaba Cloud Security obtains disk information.

Disk information instance description

In this program, we will generate an application that can obtain the current disk information, similar to the result obtained by right-clicking the disk "attribute. After running, select the disk to obtain the serial number, volume label, file type, used space, unused space, and disk capacity of the current disk.

Technical Points

L determine the drive type

L obtain disk Information

L obtain the disk space and total space

Implementation Process ■ create a project
Open Visual Studio. NET, select "New Project", select "Visual Basic Project" in the project type window, select "Windows application" in the template window, and enter "DiskInfo" in the name field ", then select the Save path. Click OK ".

■ Add controls
Add sixteen Label controls, one Picture control, one ComboBox control, and one Line control to the current form.

■ Set attributes
Based on the interface of the form, change the Text attribute of the descriptive Label control to the same as that of the interface. Some main attributes are listed in Table 96-1. For other attributes, refer to the CD.

Table 96-1 attribute values of forms and controls

Forms/controls

Attribute

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 to 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 "Parameters" (ByVal lpRootPathName As String, ByVal encoded As String, ByVal nVolumeNameSize As Integer, ByRef encoded 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 l1_ctorspercluster As Integer, ByRef passed As Integer) As Integer

End Module

The following code is added to the form:

Private Sub combow.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 disk drive type

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 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

'The remaining disk space, used space, and total space are calculated

X = GetDiskFreeSpace (buff, lppercluster, lpBytesPerSector, lpfree, lptotal)

Freespace = lpfree * lpBytesPerSector * lppercluster

Totalspace = lptotal * lpBytesPerSector * CDbl (lppercluster)

'Totalspace = totalspace * 10

LblUnusedSpace. Text = CStr (freespace) & "Byte" & CStr (Int (freespace/1024/1024) & "MB"

LblTotalVolume. Text = CStr (totalspace) & "Byte" & CStr (Int (totalspace/1024/1024) & "MB"

LblUsedSpace. Text = CStr (totalspace-freespace) & "Byte" & CStr (Int (totalspace-freespace)/1024/1024) & "MB"

Lblintro. Text = "Drive" & Combo1.Text

End Sub

'When 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 combow.selectedindexchanged (Combo1, New System. EventArgs ())

End Sub

■ Run the program
Click debug | start or click the icon to run the program.

Summary
We can use these three GetDriveType (), etVolumeInformation (), and etDiskFreeSpace () API functions to obtain information about disks. The GetDriveType () function is used to obtain information about the disk drive type. The GetVolumeInformation () function is used to obtain information about the disk volume label. GetDiskFreeSpace () is used to obtain information about the remaining disk space and total disk space.


 

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.