I have written some code which should store some strings out of a string-array for me line by line in a file, sadly I have to do this for about 200k strings in an array and which takes me forever...
foreach (var file in fileArray) {
// Console.WriteLine(file);
string sub = file.Substring(49,file.Length - 49);
using (StreamWriter sw = File.AppendText(folder + ".tfcs"))
{
sw.WriteLine(sub);
}
indexfiles++;
Console.Clear();
Console.WriteLine("Scanning Folder:");
Console.WriteLine(folder);
Console.WriteLine("Filesfound:" + fileArray.Length);
Console.WriteLine("Fileswritten to Database:" + indexfiles);
}
}
Is there any way to improve this?
P.S. I have get the progress for the amount of strings already written to the file...
You're opening and closing the file on every iteration of the loop. That's almost certainly the problem, or at least part of it.
Instead of that, open it once:
using (var writer = File.AppendText(folder + ".tfcs"))
{
foreach (var file in fileArray)
{
writer.WriteLine(file.Substring(49, file.Length - 49));
indexFiles++;
}
}
Console.Clear();
Console.WriteLine("Scanning Folder:");
Console.WriteLine(folder);
Console.WriteLine("Files found:" + fileArray.Length);
Console.WriteLine("Files written to Database:" + indexFiles);
I've also moved the console output to the end - clearing the console and writing to it on every iteration is almost certainly slowing things down very significantly too. If you need some sort of progress report as you go, I'd do it every 100 lines or something like that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With