Rubrik: Dateisystem · Laufwerke | VB-Versionen: VB4, VB5, VB6 | 10.03.01 |
Skalierte Anzeige des freien Speicherplatzes Skalierte Anzeige der auf einem Laufwerk zur Verfügung stehenden Gesamt- und Rest-Speicherkapazität | ||
Autor: Helmut Suhren | Bewertung: | Views: 15.012 |
ohne Homepage | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Nachfolgender Tipp ermittelt den gesamten und den noch verfügbaren Speicherplatz eines bestimmten Laufwerks. Die Funktion GetFreeDiskSpace formatiert hierbei die ermittelten Werte in folgende Form: 1,63 GB frei (von 2,44 GB)
' zunächst alle benötigten API-Deklarationen Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" _ Alias "GetDiskFreeSpaceExA" ( _ ByVal lpDirectoryName As String, _ lpFreeBytesAvailableToCaller As Currency, _ lpTotalNumberOfBytes As Currency, _ lpTotalNumberOfFreeBytes As Currency) As Long Private Function GetDiskFreeSpace(Drive As String, _ BytesAvailable As Currency, TotalBytes As Currency, _ FreeBytes As Currency) As Boolean GetDiskFreeSpace = GetDiskFreeSpaceEx(Drive, _ BytesAvailable, TotalBytes, FreeBytes) If GetDiskFreeSpace Then BytesAvailable = BytesAvailable * 10000 TotalBytes = TotalBytes * 10000 FreeBytes = FreeBytes * 10000 End If End Function Private Function Log10(X As Currency) As Currency Log10 = IIf(X = 0, 0, Log(X) / Log(10#)) End Function Private Function xformat(a As Double, _ ByVal fmt As String) As String xformat = Format(a, fmt) End Function ' Ermittelt den Speicherplatz ' Rückgabewert (String) ' ' Beispiele: x,xx GB frei (von x,xx GB) ' x,xx MB frei (von x,xx GB) ' x,xx KB frei (von x,xx MB) Public Function GetFreeDiskSpace(Dir As String) As String Dim Drive As String Dim BytesAvailable As Currency Dim TotalBytes As Currency Dim FreeBytes As Currency Dim EinheitName As Variant Dim EinheitFormat As Variant EinheitName = Array("Byte", "Byte", "Byte", "KB", "KB", _ "KB", "MB", "MB", "MB", "GB", "GB", "GB", "TB", "TB", "TB") EinheitFormat = Array("0", "0", "0", "0.00", "0.0", "0", _ "0.00", "00.0", "0", "0.00", "0.0", "0", "0.00", "0.0", "0") Dim MB As Integer Dim j As Integer Dim k As Integer Drive = Left(Dir, 2) & "\" If GetDiskFreeSpace(Drive, BytesAvailable, TotalBytes, _ FreeBytes) Then j = Int(Log10(TotalBytes)) k = Int(Log10(FreeBytes)) GetFreeDiskSpace = xformat(FreeBytes / 1024 ^ Int(k / 3), _ EinheitFormat(k)) & " " & EinheitName(k) & _ " frei (von " & xformat(TotalBytes / 1024 ^ Int(j / 3), _ EinheitFormat(j)) & " " & EinheitName(j) & ")" Else GetFreeDiskSpace = "??" End If End Function
Um den Tipp auszuprobieren, starten Sie ein neues Projekt, plazieren auf der Form ein Eingabe-Steuerelement Text1, sowie eine Schaltfläche Command1. Im Click-Ereignis von Command1 fügen Sie folgenden Code ein:
Private Sub Command1_Click() Text1.Text = GetFreeDiskSpace(CurDir) End Sub