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
That approach did not work for me. I may have some circumstance that is a little different. The "continue;" command ended up being the solution for me. The exception was being thrown in a method that is called from my foreach loop. I handle the ex in that method; return a string (type that the method should return; then use the "continue" word in the foreach loop.
- I have this code in my loop. NOTE: the GetWsResults method is where it was choking.