How to get all the files, sub files and their size inside a directory in C#?

To get all files and subdirectories within a directory in C#, the Directory.GetFiles method provides a comprehensive solution. This method returns the names of all files (including their full paths) that match a specified search pattern and can optionally search through subdirectories.

The FileInfo class allows you to retrieve detailed information about each file, including its size, creation date, and other properties.

Syntax

Following is the syntax for using Directory.GetFiles

string[] files = Directory.GetFiles(path, searchPattern, searchOption);

Parameters

  • path − The directory path to search

  • searchPattern − The search pattern (e.g., "*.*" for all files, "*.txt" for text files)

  • searchOption − Specifies whether to search subdirectories

SearchOption Description
TopDirectoryOnly Searches only the specified directory
AllDirectories Searches the specified directory and all subdirectories

Getting All Files from Directory and Subdirectories

This example demonstrates how to retrieve all files from a directory and its subdirectories −

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      string rootPath = @"C:\Temp\TestFolder";
      
      // Create test directory structure for demonstration
      Directory.CreateDirectory(rootPath);
      Directory.CreateDirectory(Path.Combine(rootPath, "SubFolder"));
      File.WriteAllText(Path.Combine(rootPath, "file1.txt"), "Hello World");
      File.WriteAllText(Path.Combine(rootPath, "SubFolder", "file2.txt"), "Sample content");
      
      var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);

      Console.WriteLine("All files in directory and subdirectories:");
      foreach (string file in files) {
         Console.WriteLine(file);
      }
   }
}

The output of the above code is −

All files in directory and subdirectories:
C:\Temp\TestFolder\file1.txt
C:\Temp\TestFolder\SubFolder\file2.txt

Getting Files from Top Directory Only

This example shows how to retrieve files only from the specified directory, excluding subdirectories −

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      string rootPath = @"C:\Temp\TestFolder";
      
      // Create test files
      Directory.CreateDirectory(rootPath);
      Directory.CreateDirectory(Path.Combine(rootPath, "SubFolder"));
      File.WriteAllText(Path.Combine(rootPath, "topfile.txt"), "Top level file");
      File.WriteAllText(Path.Combine(rootPath, "SubFolder", "subfile.txt"), "Sub level file");
      
      var files = Directory.GetFiles(rootPath, "*.*", SearchOption.TopDirectoryOnly);

      Console.WriteLine("Files in top directory only:");
      foreach (string file in files) {
         Console.WriteLine(file);
      }
   }
}

The output of the above code is −

Files in top directory only:
C:\Temp\TestFolder\topfile.txt

Getting Files with Size Information

This example demonstrates how to retrieve files along with their size information using the FileInfo class −

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      string rootPath = @"C:\Temp\TestFolder";
      
      // Create test files with different sizes
      Directory.CreateDirectory(rootPath);
      File.WriteAllText(Path.Combine(rootPath, "small.txt"), "Hi");
      File.WriteAllText(Path.Combine(rootPath, "medium.txt"), "This is a longer text file with more content.");
      File.WriteAllText(Path.Combine(rootPath, "large.txt"), new string('A', 1000));
      
      var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);

      Console.WriteLine("Files with size information:");
      foreach (string file in files) {
         var info = new FileInfo(file);
         Console.WriteLine($"{Path.GetFileName(file)}: {info.Length} bytes");
      }
   }
}

The output of the above code is −

Files with size information:
small.txt: 2 bytes
medium.txt: 46 bytes
large.txt: 1000 bytes

Getting Directory Structure with Total Size

This example shows how to calculate the total size of all files in a directory structure −

using System;
using System.IO;
using System.Linq;

class Program {
   static void Main(string[] args) {
      string rootPath = @"C:\Temp\TestFolder";
      
      // Create test directory structure
      Directory.CreateDirectory(rootPath);
      Directory.CreateDirectory(Path.Combine(rootPath, "Docs"));
      File.WriteAllText(Path.Combine(rootPath, "readme.txt"), "Project readme file");
      File.WriteAllText(Path.Combine(rootPath, "Docs", "manual.txt"), "User manual content");
      
      var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);
      long totalSize = 0;

      Console.WriteLine("Directory structure:");
      foreach (string file in files) {
         var info = new FileInfo(file);
         totalSize += info.Length;
         Console.WriteLine($"{file}: {info.Length} bytes");
      }
      
      Console.WriteLine($"\nTotal files: {files.Length}");
      Console.WriteLine($"Total size: {totalSize} bytes");
   }
}

The output of the above code is −

Directory structure:
C:\Temp\TestFolder\readme.txt: 19 bytes
C:\Temp\TestFolder\Docs\manual.txt: 20 bytes

Total files: 2
Total size: 39 bytes

Conclusion

The Directory.GetFiles method with SearchOption.AllDirectories efficiently retrieves all files from a directory and its subdirectories. Combined with FileInfo, you can easily access file properties like size, making it simple to analyze directory structures and calculate storage usage.

Updated on: 2026-03-17T07:04:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements