Get Directory Parallel - CSharp File IO

CSharp examples for File IO:Directory

Description

Get Directory Parallel

Demo Code


using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System.IO;/*from w w w  . jav  a  2s . co  m*/
using System;

public class Main{
        public static long GetDirectorySizeParalell(string sourceDir, bool recurse)
      {
         long size = 0;
         string[] fileEntries = Directory.GetFiles(sourceDir);

         foreach (string fileName in fileEntries)
         {
            Interlocked.Add(ref size, (new FileInfo(fileName)).Length);
         }

         if (recurse)
         {
            string[] subdirEntries = Directory.GetDirectories(sourceDir);

            Parallel.For<long>(0, subdirEntries.Length, () => 0, (i, loop, subtotal) =>
            {
               if ((File.GetAttributes(subdirEntries[i]) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
               {
                  subtotal += GetDirectorySizeParalell(subdirEntries[i], true);
                  return subtotal;
               }
               return 0;
            },
               (x) => Interlocked.Add(ref size, x)
            );
         }
         return size;
      }
}

Related Tutorials