VB App won't populate ListBox with files found with GetFiles

Private Sub findPSTButton_Click(sender As Object, e As EventArgs) Handles findPSTButton.Click
        BackupperProgress.Value = 0
        MasterTimer.Start()
        Try
            For Each foundPST As String In My.Computer.FileSystem.GetFiles(
                Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.pst")
                PSTList.Items.Add(foundPST)
            Next
        Catch ex As Exception

        End Try
        BackupperProgress.Value = 99
    End Sub

Open in new window


According to several sources, this should do the following:

a) Start the progress bar
b) Search the current logged in User Profile folder for PST files.
c) Ignore Junction Points (i.e. My Pictures under Documents, etc) and all exceptions
d) Add all of the PST's found to the ListBox

Unfortunately, when the button is clicked nothing appears in the list box. Can anyone tell me what I've done wrong here?

EDIT: By adding a MsgBox(ex.Message) I found out that I'm still getting access denied errors. I've tried putting a Catch line after the For Each one but it blocks the foundPST variable from being used elsewhere in the code. It also says that a Next line needs to be before the Catch. But if I do that I'm back to square one.
LVL 1
Jason ParadisIT Operations SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Jason;

Try changing this line of code
PSTList.Items.Add(foundPST)

Open in new window

To this
 PSTList.Items.AddRange(foundPST.ToArray())

Open in new window

1
NorieAnalyst Assistant Commented:
Jason

I think you might need to separate things out a bit.

For example, first find all the subdirectories you want to search and then try and search them individually for PST files.

Doing it that way means you'll be able to deal with folders where there are problems with access.

Something like this, which I'm sure can be written more elegantly.
    Private Sub findPSTButton_Click(sender As Object, e As EventArgs) Handles findPSTButton.Click

        Dim dirs As Object

        dirs = My.Computer.FileSystem.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))

        For Each dir As String In dirs
            Try
                For Each foundPST As String In My.Computer.FileSystem.GetFiles(dir, FileIO.SearchOption.SearchAllSubDirectories, "*.pst")
                    PSTList.Items.Add(foundPST)
                Next foundPST
            Catch ex As Exception

            End Try

        Next dir

    End Sub

Open in new window

1
Jason ParadisIT Operations SpecialistAuthor Commented:
Norie,

Thank you! That worked.

However, it only shows one PST file in the ListBox when I know for a fact there are more in the User Profile folder that's being searched.

Is there a reason only one line appears? It's a pretty big box.

It found the one located in %userprofile%\Downloads\ but not the one located in %userprofile%\My Documents\ unless there is a limitation on how many rows can be in a ListBox.
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Jason ParadisIT Operations SpecialistAuthor Commented:
Fernando,

I tried your suggestion, however it produced an error:

Overload resolution failed because no accessible AddRange can be called with these arguments.
0
NorieAnalyst Assistant Commented:
Jason

Is the missing PST file definitely in the My Documents (or Documents) folder and not a subfolder of that document?

As it is the code I posted is only going to search the first level of subfolders of the user profile folder.

Do you want to search recursively? i.e. search the main folder, then it's subfolders, then the subfolders of the subfolders and so on.
0
Jason ParadisIT Operations SpecialistAuthor Commented:
Norie,

Yes, I wish to search all subdirectories as well. I thought SearchAllSubDirectories did just that.
0
NorieAnalyst Assistant Commented:
jason

Try this, it worked for me but I'm sure there's a more elegant/better solution.
Imports System.IO
Imports System.Environment

Public Class Form1

    Private Sub findPSTButton_Click(sender As Object, e As EventArgs) Handles findPSTButton.Click

        Dim dirstack As New Stack(Of String)
        Dim res As New List(Of String)

        dirstack.Push(GetFolderPath(SpecialFolder.UserProfile))

        Do While (dirstack.Count > 0)

            Dim dir As String = dirstack.Pop

            Try
                res.AddRange(Directory.GetFiles(dir, "*.pst"))

                For Each subdir As String In Directory.GetDirectories(dir)
                    dirstack.Push(subdir)
                Next subdir

            Catch ex As UnauthorizedAccessException

            End Try

        Loop

        For Each fil As String In res
            PSTList.Items.Add(fil)
        Next fil

        MsgBox(PSTList.Items.Count & " PST files found.")

    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason ParadisIT Operations SpecialistAuthor Commented:
Norie,

That is absolutely fantastic! Thank you very much, works like a charm!

And on top of that, I learned a bunch of new stuff about programming in VB. Win win!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.