Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Created March 13, 2019 01:55
Show Gist options
  • Save IngIeoAndSpare/2a03d852249a814c02a16b53b9462893 to your computer and use it in GitHub Desktop.
Save IngIeoAndSpare/2a03d852249a814c02a16b53b9462893 to your computer and use it in GitHub Desktop.
dir file checker
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace fileChecker
{
internal class Program
{
public static void Main(string[] args)
{
string path = @""; // example : @"Z:\"
//check dir size
Task directorySizeCheck = Task.Factory.StartNew(() =>
{
DirectorySize(new DirectoryInfo(path + level), true);
});
//check file length
Task fileLengthCheck = Task.Factory.StartNew(() =>
{
DirectoryFileLength(new DirectoryInfo(path + level), level);
});
//check dir length
Task directoryLengthCheck = Task.Factory.StartNew(() =>
{
DirectoryLength(new DirectoryInfo(path + level), level);
});
//wait all checker work end
Task.WaitAll(new Task[3]{directorySizeCheck, fileLengthCheck, directoryLengthCheck});
}
public static double DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
{
double totalSize = dInfo.EnumerateFiles().Sum(file => file.Length);
if (includeSubDir) totalSize += dInfo.EnumerateDirectories().Sum(dir => DirectorySize(dir, true));
return totalSize;
}
public static void DirectoryLength(DirectoryInfo dInfo, string level)
{
Console.WriteLine(level+ " level fileLength : " + dInfo.GetDirectories().Length);
}
public static void DirectoryFileLength(DirectoryInfo dInfo, string level)
{
int fileLength = Directory.GetFiles(dInfo.FullName, "*.*", SearchOption.AllDirectories).Length;
Console.WriteLine(level + " level fileLength : " + fileLength);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment