VB.NET Gets several ways to get hard disk information

zhaozj2021-02-17  49

Several methods of getting hard disk information under VB.NET

1. Get disk space with API functions getDiskFreespaceEx

Private Declare Function GetDiskFreespaceEx lib "kernel32" Alias ​​"getDiskFreespaceexa" _

(Byval lpdirectoryname as string, byref lpfreebytesavailabletocaller as long, _

BYREF LPTOTAALNUMBEROFBYTES AS Long, Byref LPTOTALNUMBEROFFREEBYTES As Long

Private sub btnDisk_Click (Byval e as system.EventArgs) Handles btnDisk.click

DIM BYTESFREETOCALLER AS Long, Totalbytes As Long

DIM TOTALFREEBYTES AS Long, Totalbytesused As Long

DIM STRESULT AS STRING

Const rootpathname = "c: /"

Call GetDiskFreespaceEx (rootpathname, bytesfreetocalller, totalbytes, totalfreebytes)

Strresult = "Drive" & "C: /" & VBCRLF

Strresult = "Disk Capacity (MB):" & Format (CDBL (Totalbytes / 1024) / 1024), "###, ###, ## 0.00") & vbcrlf

Strresult = "Available Space (MB):" & Format ((TotalFreebytes / 1024) / 1024), "###, ###, ## 0.00") & vbcrlf

Strresult = "Space (MB):" & Format ((Totalbytes - TotalFreebytes / 1024) / 1024), "###, ###, ## 0.00") & vbcrlf

Msgbox (Strresult)

End Sub

2. Implement the FSO object model with FSO (file system object model) is included in the Scripting Type Library (Scrrun.dll). The calling method is as follows: Select a reference in the project menu, select Microsoft Scripting Runtime in COM Add IMPORTS scripting at the top of the code, add the following code to the click event:

IMPORTS scripting

Private sub btnfso_click (Byval E AS System.EventArgs) Handles Btnfso.Click

DIM FSO AS New FileSystemObject

Dim Drvdisk As Drive, Strresult As String

DRVDisk = fso.getdrive ("c: /")

Strresult = "Drive" & "C: /" & VBCRLF

Strresult = "Disk volume:" & drvdisk.voluMename & vbcrlfstrresult = "disk serial number:" & drvdisk.serialnumber & vbcrlf

Strresult = "Disk Type:" & DRVDisk.driveType & Vbcrlf

Strresult = "file system:" & drvdisk.filesystem & vbcrlf

Strresult = "Disk Capacity (G):" & FormatNumber (((DRVDisk.Totalsize / 1024) / 1024) / 1024, 2,, Microsoft.VisualBasic.tristate.true) & Vbcrlf

Strresult = "Available Space (G):" & FormatNumber ((DRVDisk.Freespace / 1024) / 1024) / 1024, 2,, Microsoft.VisualBasic.tristate.true) & vbcrlf

Strresult = "Used space (G):" & formatNumber (((DRVDisk.Totalsize - Drvdisk.Freespace) / 1024) / 1024) / 1024), 2,, Microsoft.visualBasic.tristate.true

Msgbox (Strresult)

End Sub

3. Get the logical disk serial number with the API function getVolumeInformation

Private Declare Function GetVolumeInformation LIB "kernel32" Alias ​​"getVolumeinformationa" _

(Byval lprootpathname as string, byval lpvolumenamebuffer as string, byval _

NVoluMenameSize As INTEGER, BYREF LPVOLUMESERIALNUMBER AS Long, _

Byval lpmaximumcomponentlength as integer, byval lpfilesystemflags as integer, byval_

LPFILESYSTEMNAMEBUFFER AS STRING, BYVAL NFILESYSTEMNAMESIZE AS INTEGER AS INTEGER

Private sub button2_click (Byval e as system.Object, byval e as system.eventargs) Handles Button2.click Dim SerialNumber As Long

DIM Tempstr1 As New String (CHR (0), 255)

Dim Tempstr2 As New String (CHR (0), 255)

Dim Tempint1, Tempint2 AS Integer

GetVolumeInformation ("C: /", TempStr1, 256, SerialNumber, Tempint1, Tempint2, TempStr2, 256)

MSGBOX ("C disk serial number:" & serialnumber) End Sub

4, using WMI to get hard disk information Windows Management Instrumentation (WMI) is a scalable system management structure, which uses a unified, standard, scalable object-oriented interface. WMI provides you with standard methods for interacting with system management information and basic WMI API. WMI is mainly used by system management application developers and administrators to access and operating system management information. We need to implement classes provided under the system.management namespace in the .NET FRAMWORK. Imports System.Management

Private sub button3_click (byval sender as system.object, byval e as system.eventargs) Handles Button3.click

DIM Disk as managementBaseObject

DIM STRESULT AS STRING

DIM DiskClass = New ManagementClass ("Win32_LogicalDisk")

DIM Disks as managementObjectCollection

Disks = diskclass.getInstances ()

For Each Disk in Disks

Strresult = ""

Strresult = "Device ID:" & Disk ("DeviceID") & vbcrlf Strresult = "Disk Name:" & Disk ("Name" & vbcrf StrRRESULT = "Disk Terminal:" & Disk ("Volumename") & VBCRLF if Disk ("FileSystem") <> "" "" "" "" "& Disk (" FileSystem ") & vbcrf strRRESULT =" Disk Description: "& Disk (" Description ") & vbcrlf if system .Convert.toint64 ("Size")> 0 THEN STRESULT = "Disk Size:" & System.Convert.Toint64 ("size"). Tostring ()) & vbcrlf Strresult = "Disk type: "& System.convert.toint16 (Disk (" drivetype "). TOSTRING ()) endiff

Msgbox (Strresult)

NEXT

End Sub

Summary: In VB.NET, use the API function to get the hard disk information. It turns out that the API function VB6 programmer can be called after appropriate changes to the API function declaration. You can also obtain disk information using the Scrrun.dll of FSO (file system object). In .NET FRAMWORK, WMI can obtain more information about machine hardware (refer to System.Management Name Space).

Disclaimer: The right to copyright and interpretation of this article belongs to Li Honggen, if you need to reprint, please keep your full content and this statement. QQ: 21177563

MSN: lihonggen@hotmail.com

Column: http://www.9cbs.net/develop/author/netauthor/lihonggen0/0/

转载请注明原文地址:https://www.9cbs.com/read-28440.html

New Post(0)