I am currently coding on a personal project for sneaking throug binary trees and searching for new or changed files. I want to save all files, my search has found with path and md5 checksum into a csv file for the comparison afterwords. The files are loaded in a IEnumerable Variable as Objects of my own Class iFile. But the writing of the csv file takes about 5min for just 15.000 files.(1min and 6sec for processing the IEnumerable to List) Is there a way to speed up my code?
This is my recursive search:
public static IEnumerable<iFile> GetAllFiles(string root, bool ignoreUnauthorizedAccess = true)
{
Stack<string> stack = new Stack<string>();
stack.Push(root);
while (stack.Count > 0)
{
string curDir = stack.Pop();
string[] files = null;
try
{
files = Directory.GetFiles(curDir);
}
catch (UnauthorizedAccessException)
{
if (!ignoreUnauthorizedAccess) throw;
}
catch (IOException)
{
if (!ignoreUnauthorizedAccess) throw;
}
if (files != null)
foreach (string file in files)
{
iFile f = new iFile(new FileInfo(file));
yield return f;
}
string[] dirs = null;
try
{
dirs = Directory.GetDirectories(curDir);
}
catch (UnauthorizedAccessException)
{
if (!ignoreUnauthorizedAccess) throw;
}
catch (IOException)
{
if (!ignoreUnauthorizedAccess) throw;
}
if (dirs != null)
foreach (string dir in dirs)
stack.Push(dir);
}
}
This is my writing function:
private static void writeToSystem<iFile>(this IEnumerable<iFile> files, string path = "c:\")
{
using (System.IO.StreamWriter f = new System.IO.StreamWriter(path))
{
foreach (var i in files)
{
f.WriteLine(i.getPath() + ";" + i.getHash());
}
}
}
And the getHash function from the iFile class:
using (var md5 = new MD5CryptoServiceProvider())
{
if(File.Exists(@filename) && fInfo.Length < 100000 ){
try
{
byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(filename),0,2000);
return BitConverter.ToString(data);
}
catch (Exception)
{
Program.logger.log("Fehler beim MD5 erstellen!", Program.logger.LOG_ERROR);
return "";
}
} else {
return "";
}
}