FileSystemTree.Folder.GetSize C# (CSharp) Method

GetSize() public method

public GetSize ( ) : long
return long
        public long GetSize()
        {
            long sum = 0;
            foreach (Folder folder in this.ChildFolders)
            {
                sum += folder.GetSize();
            }

            foreach (File file in this.Files)
            {
                sum += file.Size;
            }

            return sum;
        }
    }

Usage Example

        internal static void Main()
        {
            string decorationLine = new string('-', Console.WindowWidth);
            Console.Write(decorationLine);
            Console.WriteLine("***Implementing a file system starting from C:\\WINDOWS with");
            Console.WriteLine("operation of getting the total size of a folder by choice***");
            Console.Write(decorationLine);

            string rootFolderName = "WINDOWS";
            Folder rootFolder = new Folder(rootFolderName);
            string startPath = "C:";

            Console.Write("Building the file system. Takes some time...");
            BuildFileSystem(rootFolder, startPath);
            Console.WriteLine("\nBuild completed!");

            StringBuilder fileSystemString = new StringBuilder();
            BuildFileSystemString(rootFolder, fileSystemString);

            using (StreamWriter writer = new StreamWriter(FileSystemDestination))
            {
                writer.Write(fileSystemString);
            }

            Console.WriteLine(
                "{0}Look file '{1}' at project's directory to see the \nfile system string representation",
                Environment.NewLine,
                FileSystemDestination.Substring(FileSystemDestination.LastIndexOf('/') + 1));

            Console.WriteLine(
                "{0}The size of the '{1}\\{2}' folder is: {3} bytes",
                Environment.NewLine,
                startPath,
                rootFolderName,
                rootFolder.GetSize().ToString("0,0", CultureInfo.InvariantCulture));

            string folderName = "Fonts";
            Folder folder = GetFolderFromFileSystem(folderName, rootFolder);
            Console.WriteLine("\nThe size of the '{0}' folder in {1}\\{2} is: {3} bytes",
                folder.Name,
                startPath,
                rootFolderName,
                folder.GetSize().ToString("0,0", CultureInfo.InvariantCulture));
        }