Bestands grootte in weergeven in form lukt niet

Status
Niet open voor verdere reacties.

hanonymouss

Gebruiker
Lid geworden
7 sep 2011
Berichten
283
Hallo allemaal

Ik heb deze code gebruikt om de grootte van de folder weer te geven in MB

[CPP] Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
ByVal includeSubDir As Boolean) As Long
Dim totalSize As Long = dInfo.EnumerateFiles() _
.Sum(Function(file) file.Length)

If includeSubDir Then
totalSize += dInfo.EnumerateDirectories() _
.Sum(Function(dir) DirectorySize(dir, True))
End If
Return totalSize
End Function

[/CPP]

[CPP] Dim dInfo As New DirectoryInfo("c:\test\")
Dim sizeOfDir As Long = DirectorySize(dInfo, True)
MsgBox("bestands grootte in MB : " & _
"{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))

[/CPP]

Als ik het debug dan krijg ik zoiets ( MsgBox("bestands grootte in MB : {0:N2} MB

dit werkt dan wel in een console
 
normaal krijg ik als resultaat de bestandsgrootte zoals in een console
 
Laatst bewerkt:
Wat bedoel je met 'de bestandsgrootte zoals in een console' ? :confused:
 
Dit krijg in een console. Dit moet ook krijgen in een form en laat het bv zien in een label of msgbox.
 
Laatst bewerkt:
Code:
Private Function DirSize(ByVal dir_name As String) As Double
        Dim total_size As Double
        Dim file_name As String

        If Right(dir_name, 1) <> "\" Then dir_name = dir_name _
            & "\"

        file_name = Dir$(dir_name)
        Do While Len(file_name) > 0
            total_size = total_size + FileLen(dir_name & _
                file_name)
            file_name = Dir$()
        Loop

         DirSize = (total_size / 1024) / 1024
    End Function

    Private _right As String
    Private Property Right(ByVal dir_name As String, ByVal p2 As Integer) As String
        Get
            Return _right
        End Get
        Set(ByVal value As String)
            _right = value
        End Set
    End Property
 
Laatst bewerkt:
Ook een poging gewaagd, je krijgt 'm trouwens als bytes terug.

[cpp]Function DirSize(ByVal Directory As String, ByVal Recursive As Boolean) As Long
Dim size As Long = 0
Try
For Each file As IO.FileInfo In New IO.DirectoryInfo(Directory).GetFiles
size += file.Length
Next
If Recursive = True Then
For Each Subdir As IO.DirectoryInfo In New IO.DirectoryInfo(Directory).GetDirectories
size += DirSize(Subdir.FullName, True)
Next
End If
Catch ex As Exception
Return 0
End Try
Return size
End Function[/cpp]
 
[cpp] Function DirSize(ByVal Directory As String) As Long
Dim size As Long = 0
Try
For Each file As IO.FileInfo In New IO.DirectoryInfo(Directory).GetFiles
size += file.Length
Next
Catch ex As Exception
Return 0
End Try
Return (size / 1024) / 1024
End Function[/cpp]
 
Laatst bewerkt:
Toch bedankt voor jullie code.

Ik heb toch de code van JoZ1 gekozen

Dim t As String

t = DirSize("c:\test\", True)
Dim intMegaBytes As Int32 = (t) \ 1048576
MsgBox("De bestandsgrootte van c:\test\: " & intMegaBytes & " MB")



Ik heb ook een code toegevoegd dat het bestand converteert naar MB

vb.png


JoZ1: wat is het verschil tussen die False en True? ik heb de waarde allebei geprobeerd en krijg hetzelfde resultaat als de foto
 
Laatst bewerkt:
Het tweede argument houdt in of hij ook de submappen meerekent ;)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan