【发布时间】:2020-05-16 06:16:56
【问题描述】:
我正在使用 vb.net。 我想问如何通过过滤器获取每个文件的文件大小? 我想获得所有大小的 .ts 文件
这是我正在使用的代码
Dim TotalSize As Long = 0
Sub FileSize()
Dim TheSize As Long = GetDirSize(txtPath.Text)
TotalSize = 0 'Reset the counter
If TheSize < 1024 Then
lblSize.Text = Math.Round(TheSize, 0) & " B"
ElseIf TheSize > 1024 AndAlso TheSize < (1024 ^ 2) Then
lblSize.Text = Math.Round(TheSize / 1024, 1) & " KB"
ElseIf TheSize > (1024 ^ 2) AndAlso TheSize < (1024 ^ 3) Then
lblSize.Text = Math.Round(TheSize / 1024 / 1024, 1) & " MB"
ElseIf TheSize > (1024 ^ 3) AndAlso TheSize < (1024 ^ 4) Then
lblSize.Text = Math.Round(TheSize / 1024 / 1024 / 1024, 1) & " GB"
End If
End Sub
Public Function GetDirSize(folder As String) As Long
Dim FolderInfo = New DirectoryInfo(folder)
For Each File In FolderInfo.GetFiles : TotalSize += File.Length
Next
For Each SubFolderInfo In FolderInfo.GetDirectories : GetDirSize(SubFolderInfo.FullName)
Next
Return TotalSize
End Function
【问题讨论】:
-
如果您的问题真的不需要回答,那么您应该删除它。如果值得回答但您自己提供了答案,您应该发布答案并接受它,而不是发表评论。这样,每个人都可以看到问题已经得到解答,而无需打开它并先阅读它。也就是说,我不能 100% 确定您不需要特定数量的声望点来执行这些操作。
标签: vb.net file directoryinfo