title: Current index of foreach loop
body: <p>Is there a way to know the current run of a foreach without have to:</p>
  
  <pre><code>Int32 i;
  foreach
     i++;
  </code></pre>
  
  <p>or is that the best option I got? Also, how can I know the maximum number of items on that loop? What I am trying to do is update a progressbar during a foreach loop on my form.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>    FileInfo[] directoryFiles = (new DirectoryInfo(folderBrowserDialog.SelectedPath)).GetFiles("*.*");
      foreach (FileInfo file in directoryFiles)
      {
          if ((file.Attributes &amp; FileAttributes.Hidden) == FileAttributes.Hidden || (file.Attributes &amp; FileAttributes.System) == FileAttributes.System)
              continue;
  
          backgroundWorkerLoadDir.ReportProgress(i, file.Name);
          System.Threading.Thread.Sleep(10);
      }
  </code></pre>
  
  <p>So it should be like the following, right?</p>
  
  <pre><code>   for (Int32 i = 0; i &lt; DirectoryFiles.Length; i++)
     {
         if ((DirectoryFiles[i].Attributes &amp; FileAttributes.Hidden) == FileAttributes.Hidden || (DirectoryFiles[i].Attributes &amp; FileAttributes.System) == FileAttributes.System)
             continue;
  
         backgroundWorkerLoadDir.ReportProgress((i / DirectoryFiles.Length) * 100, DirectoryFiles[i].Name);
         System.Threading.Thread.Sleep(10);
     }
  </code></pre>