Link to home
Start Free TrialLog in
Avatar of bromy2004
bromy2004Flag for Australia

asked on

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

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
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

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
Avatar of bromy2004

ASKER

It will be for Subfolders
"Z:\Reports\Jet Reports\"
to "N:\Quick Corporate\Forms\"
Do you have any system folder inside "Z:\Reports\Jet Reports\"?
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.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
...never mind... I see it already has exception support.
My bad...