![]() |
Tipp 0028
|
Verzeichnisgröße auslesen
|
 |
|
Autor/Einsender: Datum: |
|
Michael Werner 09.02.2004 |
|
Entwicklungsumgebung: |
|
VB.Net 2002 |
Framework: |
|
1.0 |
|
|
Die Größe eines Ordners in Bytes ermitteln: Mit den Klassen FileInfo und
DirectoryInfo des Namespaces System.IO können in
For...Each-Schleifen mit der DirectoryInfo.GetFiles-Methode
und der DirectoryInfo.GetDirectories-Methode die Bytes-Größen der Dateien addiert
werden (Eigenschaft FileInfo.Length) und durch einen rekursiven Aufruf der Funktion auch
alle Unterordner erfasst werden.
|
|
|
Imports System.IO
Imports System.Environment
Public Class Form1
Inherits System.Windows.Forms.Form Vom Windows Form Designer generierter Code
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim Dir As DirectoryInfo = New DirectoryInfo(TextBox1.Text)
If Dir.Exists Then
Label1.Text = "Bitte warten..."
Label1.Refresh()
Cursor.Current = Cursors.WaitCursor
Label1.Text = Format(GetDirectorySize(TextBox1.Text), _
"###,###,###,###") & " Bytes"
Cursor.Current = Cursors.Default
Else
MessageBox.Show("Falsche Pfadangabe!", "Fehler", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
End Sub
Friend Function GetDirectorySize(ByVal DirPath As String) As Long
Dim DirSize As Long
Dim Dir As DirectoryInfo = New DirectoryInfo(DirPath)
Try
Dim ChildFile As FileInfo
For Each ChildFile In Dir.GetFiles()
DirSize += ChildFile.Length
Next
Dim SubDir As DirectoryInfo
For Each SubDir In Dir.GetDirectories()
DirSize += GetDirectorySize(SubDir.FullName)
Next
Catch except As Exception
MsgBox(except.Message, MsgBoxStyle.Exclamation, "Fehler")
Exit Function
End Try
Return DirSize
End Function
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = GetFolderPath(SpecialFolder.System)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
|
|
|
|
|
Windows-Version |
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
|
Download (6,8 kB)
|
Downloads bisher: [ 1244 ]
|
|
|