title: Open a directory and process files/folders in background worker query
body: <p>I have the following code, I'm trying to open a directory and process the files in it via the Background worker but I am having issues with it.</p>
  
  <p>The error I have is (The name filePath does not exist in the current context), which I can understand because it's stored in another method? if someone could point out to me what is wrong with my code it would be appreciated. Folderbrowser doesn't work under the Background worker section.</p>
  
  <pre><code>private void btnFiles_Click(object sender, EventArgs e)
      {
          //btnFiles.Enabled = false;
          btnSTOP.Enabled = true;
          //Clear text fields
          listBoxResults.Items.Clear();
          listBoxPath.Items.Clear();
          txtItemsFound.Text = String.Empty;
          //Open folder browser for user to select the folder to scan
          DialogResult result = folderBrowserDialog1.ShowDialog();
          if (result == DialogResult.OK)
          {
              //Store selected folder path
              string filePath = folderBrowserDialog1.SelectedPath;
          }
          //Start the async operation here
          backgroundWorker1.RunWorkerAsync();
      }
  
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          //Process the folder
              try
              {
                  foreach (string dir in Alphaleonis.Win32.Filesystem.Directory.EnumerateFiles(filePath, "*.*", SearchOption.AllDirectories, true))
                  {
                      //Populate List Box with all files found
                      this.Invoke(new Action(() =&gt; listUpdate2(dir)));
                      FileInfo fi = new FileInfo(dir);
                      if (fi.Length == 0)
                      {
                          //Populate List Box with all empty files found
                          this.Invoke(new Action(() =&gt; listUpdate1(dir + Environment.NewLine)));
                      }
                  }
              }
  
              //Catch exceptions
              catch (Exception err)
              {
                  // This code just writes out the message and continues to recurse. 
                  log.Add(err.Message);
                  //throw;
              }
              finally
              {
                  //add a count of the empty files here
                  txtItemsFound.Text = listBoxResults.Items.Count.ToString();
  
                  // Write out all the files that could not be processed.
                  foreach (string s in log)
                  {
                      this.Invoke(new Action(() =&gt; listUpdate1(s)));
                  }
                  log.Clear();
                  MessageBox.Show("Scanning Complete", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
              }
              //If cancel button was pressed while the execution is in progress
              //Change the state from cancellation ---&gt; cancelled
              if (backgroundWorker1.CancellationPending)
              {
                  e.Cancel = true;
                  //backgroundWorker1.ReportProgress(0);
                  return;
              }
              //}
              //Report 100% completion on operation completed
              backgroundWorker1.ReportProgress(100);
          }
  </code></pre>