我有一個列表視圖顯示文件夾和文件,我可以顯示文件和子文件夾的大小,但我怎麼用子做只是不包括父/根文件夾。得到一個子文件夾/子目錄的大小exlcuding父文件
編輯
一樣,如果Folder1
的大小是10 MB
,它有一個SubFolder
與20 MB
大小,總的30 MB
,它應該只得到SubFolder
這是20 MB
顯示內容時的大小的Folder1
在ListView
。
Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long
Dim Size As Long = 0
Dim dis As DirectoryInfo() = d.GetDirectories()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size
End Function
我的列表視圖代碼:
Sub lv1items()
ListView1.Items.Clear()
Dim fPath As String = Form2.TextBox1.Text
Dim di = New DirectoryInfo(fPath)
' store imagelist index for known/found file types
Dim exts As New Dictionary(Of String, Int32)
If di.Exists = False Then
MessageBox.Show("Destination path" & " " & Form2.TextBox1.Text & " is not found.", "Directory Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
Form2.Show()
Else
Dim img As Image
Dim lvi As ListViewItem
For Each d In di.EnumerateDirectories("*.*", SearchOption.TopDirectoryOnly)
lvi = New ListViewItem(d.Name)
lvi.SubItems.Add(DirSize(di).ToString("0.00") & " MB")
lvi.SubItems.Add(d.CreationTime.Date)
ListView1.Items.Add(lvi)
img = NativeMethods.GetShellIcon(d.FullName)
ImageList1.Images.Add(img)
lvi.ImageIndex = ImageList1.Images.Count - 1
Next
End Sub
一點幫助嗎?
你不得不在屋裏總結文件 - 文件夾本身沒有大小 – Plutonix
@ user2041732。對不起,我沒有注意到這是vb.net。我現在已經解決了我的問題。請檢查一下。 –
我如何做到這一點與子文件夾?我的意思不是我在裏面,在'ListView'中,或不是被聲明爲'DirectoryInfo'的。 – Dhan