Avatar of Jason Paradis
Jason ParadisFlag for United States of America

asked on 

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.
Visual Basic.NET

Avatar of undefined
Last Comment
Jason Paradis
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

SOLUTION
Avatar of Norie
Norie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jason Paradis
Jason Paradis
Flag of United States of America image

ASKER

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.
Avatar of Jason Paradis
Jason Paradis
Flag of United States of America image

ASKER

Fernando,

I tried your suggestion, however it produced an error:

Overload resolution failed because no accessible AddRange can be called with these arguments.
Avatar of Norie
Norie

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.
Avatar of Jason Paradis
Jason Paradis
Flag of United States of America image

ASKER

Norie,

Yes, I wish to search all subdirectories as well. I thought SearchAllSubDirectories did just that.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Jason Paradis
Jason Paradis
Flag of United States of America image

ASKER

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!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo