Frage

I have this following method to get directory size in bytes:

    public static long GetDirectorySize(string folderPath)
    {
        DirectoryInfo di = new DirectoryInfo(folderPath);
        return di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
    }

I would like to add the following functionality - if the size of the directory is larger than 500 MB (or any input number), I would like it to return -1;

I can easily achieve it by changing the return to:

var size = di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length)
return (size <= 500*1024) ? size : -1;

This is not elegant nor efficient as it goes over all the files in the .Sum(fi => fi.Length).

Is there an elegant way to add functionally which allows the caller to add a threshold ?

Note: I am trying to avoid iterating over all the files, if there is a folder with 100,000 files and the first is larger than 500 MB, no point in continuing iteration.

War es hilfreich?

Lösung

You can misuse LINQ to do this with side-effects:

int totalSize = 0;
directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)
     .TakeWhile(fi => (totalSize += fi.Length) < threshold)
     .Count();      // Force query to execute

return (totalSize <= threshold) ? totalSize : -1;

Andere Tipps

In your method you can specify an optional argument set to 0 so if you input some value it will be set to that value, else will be 500mb. The method converts bytes to mb, and returns -1 if value is above and 1 if less:

EDIT:

public static int GetDirectorySize(string folderPath,int userinput = 0)
{
    double threshold = userinput == 0 ? 500 : userinput;
    DirectoryInfo di = new DirectoryInfo(folderPath);
    double temp = (di.EnumerateFiles("*", SearchOption.AllDirectories).TakeWhile(fl => (fl.Length / 1024f) / 1024f <= threshold).Sum(fi => fi.Length) / 1024f) / 1024f;
    temp = Math.Round(temp, 2);
    if (temp >= threshold)
    {
        return -1;
    }
    else
    {
        return 1;
    }
    // or  return temp > threshold ? -1 : 1;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top