Is there a way to know the current run of a foreach without have to:
Int32 i;
foreach
i++;
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.
This is what I have so far:
FileInfo[] directoryFiles = (new DirectoryInfo(folderBrowserDialog.SelectedPath)).GetFiles("*.*");
foreach (FileInfo file in directoryFiles)
{
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden || (file.Attributes & FileAttributes.System) == FileAttributes.System)
continue;
backgroundWorkerLoadDir.ReportProgress(i, file.Name);
System.Threading.Thread.Sleep(10);
}
So it should be like the following, right?
for (Int32 i = 0; i < DirectoryFiles.Length; i++)
{
if ((DirectoryFiles[i].Attributes & FileAttributes.Hidden) == FileAttributes.Hidden || (DirectoryFiles[i].Attributes & FileAttributes.System) == FileAttributes.System)
continue;
backgroundWorkerLoadDir.ReportProgress((i / DirectoryFiles.Length) * 100, DirectoryFiles[i].Name);
System.Threading.Thread.Sleep(10);
}