Vb. Several methods of obtaining hard disk information under net

Source: Internet
Author: User
Tags chr file system integer object model tostring
Hard disk


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/

trackback:http://tb.blog.csdn.net/trackback.aspx?postid=13650
[Click here to collect this article] published on March 09, 2004 11:44 PM


Hao Xiang published in 2004-09-06 12:22 AM ip:220.192.67.*
Here's a C # version of using WMI to get hard disk information
String strresult;

ManagementClass Diskclass = new ManagementClass ("Win32_LogicalDisk");

Managementobjectcollection disks;
disks = Diskclass.getinstances ();
foreach (ManagementObject disk in disks)
{
strresult = "";

Strresult + + "Device ID:" + disk["DeviceID"];

Strresult + = "Disk Name:" + disk["name"];

Strresult + + "Disk volume Label:" + disk["VolumeName"];

if (disk["filesystem"]. ToString ()!= "")
{
Strresult + + "file system:" + disk["filesystem"];

Strresult + = "Disk Description:" + disk["Description"];

if (System.Convert.ToInt64 (disk["Size"]) > 0)
{
Strresult + = "Disk size:" + System.Convert.ToInt64 (disk["size"]. ToString ());
}
Strresult + = "Disk type:" + System.Convert.ToInt16 (disk["DriveType"). ToString ());
}
Response.Write (Strresult);
}
This is C # code, but I encountered the issue of permission settings, do not know how to set up, do not know the GGMM to help solve the System.Management.ImpersonationLevel
Can't you join this in the web.config?
<system.web>
<identity impersonate= "true"/>
</system.web>
Error message:
Exception Details: System.Management.ManagementException: Access Denied



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.