I want to build a win service(no UI) on c# that all what it done is: run on list of directories and delete files that over then X kb. I want the better performance,
what is the better way to do this? there is no pure async function for delete file so if i want to use async await I can wrap this function like:
public static class FileExtensions {
public static Task DeleteAsync(this FileInfo fi) {
return Task.Factory.StartNew(() => fi.Delete() );
}
}
and call to this function like:
FileInfo fi = new FileInfo(fileName);
await fi.DeleteAsync();
i think to run like
foreach file on ListOfDirectories
{
if(file.Length>1000)
await file.DeleteAsync
}
but on this option the files will delete 1 by 1 (and every DeleteAsync will use on thread from the threadPool). so i not earn from the async, i can do it 1 by 1.
maybe i think to collect X files on list and then delete them AsParallel
please help me to find the better way
You can use
Directory.GetFiles("DirectoryPath").Where(x=> new FileInfo(x).Length < 1000);
to get a list of files that are under 1 KB of size.Then use Parallel.ForEach to iterate over that collection like this:
It could be argued that you should use:
to improve the readability of the code.
MSDN has a simple example on how to use Parallel.ForEach()
If you are wondering about the FileInfo object, here is the documentation
this is may be can help you.