I’ve got a little problem. I programmed a winforms application that recursivly searches files in a specified folder and its subfolders. When i run the application the first time the method needs 10 minutes for finding 100’000 files, but then when i execute the search-method a second time (without closing and reopening the windows form) it needs the double amount of time. why do it takes the double amount of time?
edit: first searchrun: 10 min, second run: 20 min, third run 40 min…
here my search-method:
public void dirsearch(string sdir)
{
this.Invoke((MethodInvoker)delegate
{
progressBar.Refresh();
dataGridView.Refresh();
// runs on UI thread
});
foreach (string d in Directory.GetDirectories(sdir))
{
try
{
foreach (string f in Directory.GetFiles(d))
{
if (Regex.IsMatch(f, searchPattern, RegexOptions.IgnoreCase))
{
FileInfo file = new FileInfo(f);
string fileName = file.Name;
string filePath = f;
string user = File.GetAccessControl(filePath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
properties.Add(new FileProperties(file.Name, f, file.Length, file.LastWriteTime));
//dataGridView.Refresh();
//progressBar.Refresh();
tryagainpoint:
cmd.CommandText = String.Format("insert into Attribute (Dateiname, Dateipfad, Dateierstellungsdatum, Dateiendung, Grösse, Ersteller, Letztes_mal_bearbeitet, FS) values ('{0}', '{1}', '{2}', '{3}', {4}, '{5}', '{6}', {7})", file.Name, f, file.CreationTime, file.Extension, file.Length, user, file.LastWriteTime, ID);
try
{
cmd.ExecuteNonQuery();
}
catch (OleDbException)
{
if (file.Name.Contains(@"'"))
{
fileName = file.Name.Replace(@"'", @"''");
filePath = f.Replace(@"'", @"''");
goto tryagainpoint;
}
}
finally
{
file = null;
}
}
}
dirsearch(d);
progressBar.MarqueeAnimationSpeed = 0;
}
catch (Exception exc) //Schreibt geworfene Exception in logfile.txt
{
WriteExceptionIntoTxtFile(exc);
continue;
}
}
}
Do you clear collection “properties” between the two calls. If not, this collection will contain 100,000 entries at the beginning of the second call to that function and contain 200,000 at the end. There is a performance issue with the data grid when you use grouping!
BTW: the code would be faster, if you don’t refresh datagrid and progressbar after each found file!