VB.net Get My Documents folder size on Win7

CraigLazar
CraigLazar used Ask the Experts™
on
Hi,
I have a small exe that i has a routine in to work out the folder size. I need to work out the folder size of My Documents. Problem is that i think windows 7 security is a problem when my exe runs as it is not picking up the default folder My Documents under c:\Users\Craig\My Documents.

thanks a lot

cheers
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

How do you specify the My Documents folder in your code?
You should use: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

Author

Commented:
Hi,
I have a class which iterates thru all the folders and sub folders on the c drive, in this process i have a simple If statement using INSTR function to look for My Documents
So it finds no match when i pass it C drive, i then have a My Documents on my D Drive which then my process picks it up.
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Most Valuable Expert 2012
Top Expert 2014

Commented:
With Win7 (and Vista may be), these folders are difficult to access directly (sometimes even in Explorer). You may have to add special conditions and logic for these folders.
I just done a little vb.net project in order to test this behaviour
The problem seams to only be with My Music and My Pictures subfolders of My Documents.

In fact looking with Windows Explorer you'll see that this subfolders event don't exists in My Documents but they are stored separatelly.

Here it is the code for getting the folder size:
 
Private Function fGetFolderSize(ByVal pstrFolder As String) As Long
        Dim lngSize As Long
        Dim di As System.IO.DirectoryInfo

        Try
            di = New System.IO.DirectoryInfo(pstrFolder)

            Try
                For Each fi As System.IO.FileInfo In di.GetFiles
                    lngSize += fi.Length
                Next
            Catch ex As UnauthorizedAccessException
                'nothing to do
            Catch ex As Exception
                Throw
            End Try
            For Each diTmp As System.IO.DirectoryInfo In di.GetDirectories
                Try

                    lngSize += fGetFolderSize(diTmp.FullName)

                Catch ex As UnauthorizedAccessException
                    'nothing to do
                Catch ex As Exception
                    Throw
                End Try
            Next
            Return lngSize
        Catch ex As Exception
            Throw
        End Try

    End Function

Open in new window


You could call this on My Documents using:
Msgbox "MyDocuments size=" & fGetFolderSize(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))

This will return the size without My Music and MyPictures

If you want this 2 folders size you could call the function for each of them:


fGetFolderSize(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic))
and
fGetFolderSize(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))

Hope this is OK

Author

Commented:
Thanks so much for the sample code man, it is perfect tweeked it slightly :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial