Exception Handling within ForEach loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Exception Handling within ForEach loop

  1. #1
    Join Date
    May 2010
    Posts
    8

    Exception Handling within ForEach loop

    Hello,

    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);
    OriginalFileArray.Add(FullFilePath);
    //add the File as another entry in FileArray
    }

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Exception Handling within ForEach loop

    Yes it's possible. But you need to make use of a recursive method.

    Lucky for you, I was a bit bored
    Code:
        private void button1_Click(object sender, EventArgs e) {
          List<string> OriginalFileArray = GetFiles("C:/temp");
        }
    
        private List<string> GetFiles(string location) {
          List<string> fileArray = new List<string>();
    
          try {
            DirectoryInfo dir = new DirectoryInfo(location);
            var files = dir.GetFiles();
            foreach (var file in files) {
              fileArray.Add(file.FullName);
            }
    
            var directories = dir.GetDirectories();
            foreach (var subdir in directories) {
              fileArray.AddRange(GetFiles(subdir.FullName));
            }
          }
          catch {
            //do nothing
          }
          return fileArray;
        }

  3. #3
    Join Date
    May 2010
    Posts
    8

    Re: Exception Handling within ForEach loop

    Thankyou, this code works great.

  4. #4
    Join Date
    Dec 2011
    Posts
    1

    Re: Exception Handling within ForEach loop

    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.

    Cursor.Current = Cursors.WaitCursor;
    var theResults = GetWsResults(Name_1, Value_1, theEndpoint);
    Cursor.Current = Cursors.Default;
    if (theResults.Contains("Error"))
    {
    listResults.Add(new aResult(intCurrentStep += 1, theResults));
    continue;
    }

    I have this code in the GetWsResults() method
    catch (SystemException ex2)
    {
    return String.Format("Error: {0}", ex2.Message);
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center