?
Solved

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

Posted on 2010-01-12
7
Medium Priority
?
2,602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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