Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using C# 3.0, your intentions may be better expressed with extension methods and IFormatProviders. In code, this could be an extension method on the FileInfo and DirectoryInfo ToString methods, so they would read something like this:</p> <pre><code>var directorySize = myDirectory.ToString("GB"); var fileSize = myFile.ToString("MB"); </code></pre> <p>The above code feels more natural for what you are trying to do.</p> <p>See how the following works for you. Some of it will need to be tested (the recursive method DirectoryInfoExtender.GetDirectorySize comes to mind). If you need to be able to write statements like <code>Console.WriteLine("{0:GB}", fileInfo)</code>, you might also consider writing an IFormatProvider as well.</p> <p>Also note that I have intentionally skimped on null checks and exception handling for these publically accessible methods.</p> <pre><code>public static class DirectoryInfoExtender { public static string ToString(this DirectoryInfo d, string format, int fractionalDigits) { double fileSize = GetDirectorySize(d); return FileSizeConverter.GetFileSizeString(fileSize, format, fractionalDigits); } public static double GetDirectorySize(DirectoryInfo d) { var files = d.GetFiles(); var directories = d.GetDirectories(); if(files.Length == 0 &amp;&amp; directories.Length == 0) { return 0; } else { double size = 0; foreach(var file in files) { size += file.Length; } foreach(var directory in directories) { size += GetDirectorySize(directory); } } return size; } } public static class FileInfoExtender { public static string ToString(this FileInfo f, string format, int fractionalDigits) { return FileSizeConverter.GetFileSizeString(f.Length, format, fractionalDigits); } } public class FileSizeConverter { public static string GetFileSizeString(double fileSize, string format, int fractionalDigits) { long divisor; string sizeIndicator; switch(format.ToLower().Trim()) { case "gb": divisor = (long)Math.Pow(2, 30); sizeIndicator = "gigabytes"; break; case "mb": divisor = (long) Math.Pow(2, 20); sizeIndicator = "megabytes"; break; case "kb": divisor = (long)Math.Pow(2, 10); sizeIndicator = "kilobytes"; break; default: divisor = 1; sizeIndicator = "bytes"; break; } return String.Format("{0:N" + fractionalDigits +"} {1}", fileSize / divisor, sizeIndicator); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload