?
Solved

Using System.IO.Directory.GetFiles in VB.NET

Posted on 2010-01-12
7
Medium Priority
?
2,668 Views
Last Modified: 2012-05-08
I'm trying to convert an Excel VBA Macro to VB.NET as a windows forms.
The general idea is to copy Local copies of the files to 2 other locations (1 as Template, another as XLS)

i thought i would try something new/simpler instead of the FileSystemObject
Files = System.IO.Directory.GetFiles(FullPath, "*.xls", IO.SearchOption.AllDirectories)

But i get an error because there are hidden/protected system files somewhere in the subfolder.
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Does anyone have any ideas?

I'm using VS 2010 Beta 2
0
Comment
Question by:bromy2004
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 26301335
If you have something like "c:\" don't do that, because you need handle that Unauthorized Access errror. Do a recursive loop and use a Try Catch block to handle the error for that directory

You have some examples in EE that you can use about recursive functions to loop in all directories
0
 
LVL 10

Author Comment

by:bromy2004
ID: 26301357
It will be for Subfolders
"Z:\Reports\Jet Reports\"
to "N:\Quick Corporate\Forms\"
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 26301454
Do you have any system folder inside "Z:\Reports\Jet Reports\"?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:bromy2004
ID: 26301526
No there shouldn't be.

I was testing on a separate Partition "F:", which contains Movies and Music.
the error came up when it tried to read a hidden AVG folder.
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 26301916
Try this way instead using a recursive method
Public Class Form1

    Private fileList As New List(Of String)

    Sub getFiles(ByVal folderPath As String)
        Try
            For Each folder As String In System.IO.Directory.GetDirectories(folderPath)
                getFiles(folder)
            Next
            fileList.AddRange(System.IO.Directory.GetFiles(folderPath, "*.xls").ToList)
        Catch ex As Exception : End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each folder As String In IO.Directory.GetDirectories("Z:\Reports\Jet Reports\", "*.*")
            getFiles(folder)
        Next

        Stop 'check the fileList now

    End Sub

End Class

Open in new window

0
 
LVL 41

Expert Comment

by:graye
ID: 26303835
jpaulino's solution is correct.   What the Framework despirately needs is a "ContinueOnError" option for their recursive File System routines... but alas, there currently isn't one... and isn't one planned for .Net 4.0
Since you were getting a permission denied before, I'd recommend you alter the example above to detect (and ignore) exception conditions.
0
 
LVL 41

Expert Comment

by:graye
ID: 26303851
...never mind... I see it already has exception support.
My bad...
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question