finish LastResort

This commit is contained in:
Paddy Xu
2017-04-24 22:17:46 +03:00
parent 431cf1f014
commit 5e70b0052f
16 changed files with 1359 additions and 1031 deletions

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace QuickLook.Plugin.LastResort
{
public static class FileHelper
{
public static void CountFolder(string root, ref bool stop, out long totalDirs, out long totalFiles,
out long totalSize)
{
totalDirs = totalFiles = totalSize = 0L;
var stack = new Stack<DirectoryInfo>();
stack.Push(new DirectoryInfo(root));
totalDirs++; // self
do
{
if (stop)
break;
var pos = stack.Pop();
try
{
// process files in current directory
foreach (var file in pos.EnumerateFiles())
{
totalFiles++;
totalSize += file.Length;
}
// then push all sub-directories
foreach (var dir in pos.EnumerateDirectories())
{
totalDirs++;
stack.Push(dir);
}
}
catch (Exception)
{
totalDirs++;
//pos = stack.Pop();
}
} while (stack.Count != 0);
}
public static string ToPrettySize(this long value, int decimalPlaces = 0)
{
const long OneKb = 1024;
const long OneMb = OneKb * 1024;
const long OneGb = OneMb * 1024;
const long OneTb = OneGb * 1024;
var asTb = Math.Round((double) value / OneTb, decimalPlaces);
var asGb = Math.Round((double) value / OneGb, decimalPlaces);
var asMb = Math.Round((double) value / OneMb, decimalPlaces);
var asKb = Math.Round((double) value / OneKb, decimalPlaces);
var chosenValue = asTb > 1
? $"{asTb} TB"
: asGb > 1
? $"{asGb} GB"
: asMb > 1
? $"{asMb} MB"
: asKb > 1
? $"{asKb} KB"
: $"{Math.Round((double) value, decimalPlaces)} bytes";
return chosenValue;
}
}
}