I am using a for loop with a declared INT based on count of items in a list. The list I am populating is dynamic based on file list in a directory. The logic should read that if the count is equal or over 1k records, load 1k records at a time, otherwise if it is lower, count the items in list and set that as a max for the for loop. However the for loop only runs 499 times and errors(image). THIS ERROR IMAGE.
Anybody know how to solve the error occurring?
Below is the CORE of the code to make it easy to follow.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Linq;
namespace ViewProductImages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//new list
List<string> filenames = new List<string>();
private void txtDirectory_TextChanged(object sender, EventArgs e)
{
//clear list - added peace of mind
filenames.Clear();
//list is populated here via Directory.Getfiles
//Sorted here kinda
filenames.Sort();
}
//Declare INT for for loop
int maxImagesToShow;
private void button1_Click(object sender, EventArgs e)
{
//Setting value for INT - IF over 1k records, set to 1k, IF under - set to actual list item count
if (filenames.Count() >= 1000)
{
maxImagesToShow = 1000;
}
else
{
maxImagesToShow = filenames.Count();
}
try
{
//for loop - loops based on value in INT
for (int i = 0; i < maxImagesToShow; i++)
{
var filename = filenames[i];
//remove current item being processed from the list(filenames)
filenames.Remove(filename);
}
}
catch (Exception ex)
{
}
}
}
}