明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

VB6編程中怎么取得硬盤分區(qū)信息

[摘要]也許你并不了解硬盤分區(qū)信息應(yīng)該包括些什么,但如果你曾經(jīng)對硬盤分過區(qū),你或許對此有所了解,在此為各位介紹一個用VB編寫的獲取硬盤分區(qū)信息的程序。在這個程序中,它將詳細(xì)地告訴你:你的硬盤總?cè)萘、分過幾個...
也許你并不了解硬盤分區(qū)信息應(yīng)該包括些什么,但如果你曾經(jīng)對硬盤分過區(qū),你或許對此有所了解,在此為各位介紹一個用VB編寫的獲取硬盤分區(qū)信息的程序。在這個程序中,它將詳細(xì)地告訴你:你的硬盤總?cè)萘、分過幾個區(qū)、每個區(qū)的總?cè)萘、及現(xiàn)在剩余的可用容量、硬盤分區(qū)表為幾位(即是FAT32還是FAT16),每個分區(qū)是幾個字節(jié)……怎么樣?夠完整詳細(xì)了吧!好的,就讓我們一起來看一下吧:
  首先做準(zhǔn)備工作:在FORM1上新建二個LABEL(LABEL1和LABEL2)一個COMMAND1命令按鈕。然后輸入以下代碼:
  Private Declare Function GetDriveType Lib
  kernel32“Alias "GetDriveTypeA(ByVal nDrive As String) As Long
  Private Declare Function GetDiskFreeSpace Lib“kernel32" Alias“GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
  Private Const DRIVE_FIXED = 3
  Private Sub Form_Load() ‘作初始化設(shè)置
  COMMAND1.Caption = “測試硬盤"
  Form1.Caption = “測試硬盤程序"
  Label1.WordWrap = True
  Label1.Caption = “"
  Label2.WordWrap = True
  Label2.Caption = “"
  End Sub
  Private Sub COMMAND1_Click()
  Dim DriveNum As Integer
  Dim TempDrive As String
  Dim X As Long
  For DriveNum = 97 To 122 Step 1 ‘檢測從A-Z(盤符)
  TempDrive = GetDriveType(Chr(DriveNum) & “:\")
  Select Case TempDrive ‘如是3則表示是硬盤,測試你有幾個盤
  Case 3: X = GetDiskSpace(Chr(DriveNum)) ‘調(diào)用子程序
  End Select
  Next DriveNum
  End Sub
  Public Function GetDiskSpace(DrivePath As String)
  Dim Drive As String
  Dim SectorsPerCluster As Long
  Dim BytesPerSector As Long
  Dim NumberOfFreeClusters As Long
  Dim TotalClusters As Long
  Dim Check As Integer
  Dim DiskSpace
  Dim diskTotal
  Static AllDiskTotal As Long
  Static NUM As Integer
  NUM = NUM + 1 ‘分幾個區(qū)的計算
  Drive = Left(Trim(DrivePath), 1) & “:\"
  Check = GetDiskFreeSpace(Drive, SectorsPerCluster, BytesPerSector, NumberOfFreeClusters, TotalClusters)
  If Check <> 0 Then
  DiskSpace = SectorsPerCluster * BytesPerSector * NumberOfFreeClusters
  ‘這是一個分區(qū)磁盤剩余空間的計算公式
  DiskSpace = Format$(DiskSpace, “###,###") ‘以規(guī)定格式顯示,如732,324,231
  diskTotal = SectorsPerCluster * BytesPerSector * TotalClusters
  ‘這是一個分區(qū)磁盤總?cè)萘康挠嬎愎?br>  diskTotal = Format$(diskTotal, “###,###")
  AllDiskTotal = AllDiskTotal + diskTotal ‘整個硬盤的總?cè)萘?br>  Label1.Caption =“你的硬盤總?cè)萘繛?” & Format$(AllDiskTotal,“###,###") &個字節(jié),即:” & Left(AllDiskTotal, 1) & . & Mid(AllDiskTotal, 2, 1) &“G,一共分了”& NUM &“個區(qū),其中:"
   Label2.Caption = Label2.Caption & UCase(DrivePath) & “盤的整個容量為:" & diskTotal &“個字節(jié)" & ",其剩余磁盤空間為:“& DiskSpace & " 個字節(jié),磁盤已FAT“& SectorsPerCluster & ",每個分區(qū)為:“& BytesPerSector & "個字節(jié)!埃 vbCrLf & vbCrLf”
  End If
  End Function
  OK!現(xiàn)在你運行一下,你是否滿意它?
  注:以上程序在中文WINDOWS98,中文VB6.0企業(yè)版中調(diào)試通過。