Vb. Several methods of obtaining hard disk information from net

Source: Internet
Author: User
Tags chr file system functions integer object model tostring
Hard disk
Vb. Several methods of obtaining hard disk information under net







1, using API function GetDiskFreeSpaceEx to obtain disk space



Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "Getdiskfreespaceexa" _

(ByVal lpdirectoryname as String, ByRef Lpfreebytesavailabletocaller as Long, _

ByRef lptotalnumberofbytes As Long, ByRef lptotalnumberoffreebytes as long) as long



Private Sub Btndisk_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btndisk.click

Dim Bytesfreetocalller as Long, totalbytes as Long

Dim totalfreebytes as Long, totalbytesused as Long

Dim Strresult 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.") & VbCrLf

Strresult + = "free Space (MB):" & Format (CDBL (totalfreebytes/1024)/1024), "###,###,# #0.") & VbCrLf

Strresult + = "used Space (MB):" & Format (CDBL (totalbytes-totalfreebytes)/1024)/1024), "###,###,# #0") & VbCrLf

MsgBox (Strresult)

End Sub





2, with FSO (file system object model) to achieve

The FSO object model is contained in the Scripting type library (Scrrun. DLL). The method is invoked as follows:

Select a reference in the Project menu and select Microsoft Scripting Runtime in COM

Add the Imports scripting at the top of the code, and add the following code to the button's Click event:



Imports scripting



Private Sub Btnfso_click (ByVal sender as System.Object, 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 Label:" & Drvdisk.volumename & VbCrLf

Strresult + = "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.TriSt Ate. True) & VbCrLf

Strresult + = "free Space (G):" & FormatNumber ((drvdisk.freespace/1024)/1024)/1024, 2,, Microsoft.VisualBasic.TriSt Ate. True) & VbCrLf

Strresult + = "used Space (G):" & FormatNumber (((drvdisk.totalsize-drvdisk.freespace)/1024)/1024), 2,, 1024 Osoft. VisualBasic.TriState.True)

MsgBox (Strresult)

End Sub





3. Use API function GetVolumeInformation to get Logical disk serial number



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 sender 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. Access to hard disk information using WMI

Windows Management Instrumentation (WMI) is a scalable system management architecture that employs a unified, standards-based, extensible object-oriented interface. WMI provides you with a standard way to interact with system management information and the underlying WMI API. WMI is used primarily by system management application developers and administrators to access and operating system management information.

We need to implement the class provided in the. NET framwork inside the System.Management namespace.



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 Strresult 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") & VbCrLf

Strresult + = "Disk volume Label:" & Disk ("VolumeName") & VbCrLf

If Disk ("filesystem") <> "Then strresult + =" File system: "& Disk (" FileSystem ") & VbCrLf

Strresult + = "Disk Description:" & Disk ("Description") & VbCrLf

If System.Convert.ToInt64 (Disk ("Size")) > 0 Then

Strresult + = "Disk Size:" & System.Convert.ToInt64 ("size"). ToString ()) & VbCrLf

Strresult + = "Disk type:" & System.Convert.ToInt16 ("DriveType"). ToString ())

End If

MsgBox (Strresult)

Next

End Sub





Summary: In vb.net, you can get the hard drive information with API functions. Originally familiar with API functions VB6 programmers, you can make appropriate changes to the API function declaration after the call. You can also obtain disk information using the Scrrun.DLL of the FSO (file system object). In. NET Framwork, you can use WMI to get more detailed information about machine hardware (refer to the System.Management namespace).



Disclaimer: This copyright and interpretation of the right to Lee Honggen all, if necessary reprint, please retain the full content and this statement.

qq:21177563

Msn:lihonggen@hotmail.com

Rubric: http://www.csdn.net/develop/author/netauthor/lihonggen0/








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.