Mit diesem Snippet ist es möglich, die Größe eines Ordners mit unterordnern zu ermitteln.
using System;
using System.IO;
namespace ViperBytes.IO
{
/// <summary>
/// Directory size class with recursive sub directory inclusion
///
/// (c) Copyright Vincent Wochnik 2007
/// </summary>
class DirectorySize
{
/// <summary>
/// Returns the size of a directory
/// </summary>
/// <param name="path">Path to directory</param>
/// <param name="includeSubDirectories">Specifyes, wheather sub directories are included</param>
/// <returns>Directory size</returns>
public static long GetDirectorySize(string path, bool includeSubDirectories)
{
long size = 0;
// get sub directories (recursive)
if (includeSubDirectories)
{
try
{
string[] subDirectories = Directory.GetDirectories(path);
foreach (string subDirectory in subDirectories)
size += GetDirectorySize(subDirectory, includeSubDirectories);
}
catch { /* what should we do??? */ }
}
// get files and add size
try
{
string[] fileNames = Directory.GetFiles(path);
foreach (string fileName in fileNames)
{
FileInfo fileInfo = new FileInfo(fileName);
size += fileInfo.Length;
}
}
catch { /* what should we do??? */ }
return size;
}
}
}
4 Kommentare zum Snippet