Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think it is much simpler to use the built-in methods on either <code>Directory</code> or <code>DirectoryInfo</code> to obtain all directories, or files, using the recursive search option:</p> <pre><code>public partial class Form1 : Form { private Action&lt;float&gt; updateProgMethod; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { updateProgMethod = UpdateProgress; } private void GetDirectorySizeAsync(string path) { backgroundWorker.RunWorkerAsync(path); } private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { DirectoryInfo di = new DirectoryInfo((string)e.Argument); di.GetTotalSize(ProgressCallback); } // Takes callbacks from the GetTotalSize() method private void ProgressCallback(float p) { // Invokes update progress bar on GUI thread: this.BeginInvoke(updateProgMethod, new object[] { p }); } // Actually updates the progress bar: private void UpdateProgress(float p) { progressBar.Value = (int)(p * (progressBar.Maximum - progressBar.Minimum)) + progressBar.Minimum; } } public static class IOExtensions { public static long GetTotalSize(this DirectoryInfo directory, Action&lt;float&gt; progressCallback) { FileInfo[] files = directory.GetFiles("*.*", SearchOption.AllDirectories); long sum = 0; int countDown = 0; for (int i = 0; i &lt; files.Length; i++) { sum += files[i].Length; countDown--; if (progressCallback != null &amp;&amp; countDown &lt;= 0) { countDown = 100; progressCallback((float)i / files.Length); } } return sum; } } </code></pre> <p>It's hard to guess progress without knowing the number of files or folders first!</p> <p>EDIT: I've improved the code a little.</p>
 

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