May 4th, 2010, 09:37 AM
Exception Handling within ForEach loop
I have a program that needs to get the file paths of all files in a given folder.
The following code goes through every file in the folder specified by txtFolderPath.Text, and adds this path to OriginalFileArray, which is a List of strings.
For the most part the code words fine, however if the Directory.GetFiles command hits a Folder that is protected, an "Access to the path 'C:\SomePath' is denied" exception is thrown.
Is there any way to ignore this error, and continue on with the next sub directory without exiting the loop? If not, are there any suitable workarounds?
foreach (var myFile in Directory.GetFiles(txtFolderPath.Text, "*.*", SearchOption.AllDirectories))
FullFilePath = Path.GetFullPath(myFile);
//add the File as another entry in FileArray
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!