Comprehensive Guide to Managing Files and Directories in C#

 Comprehensive Guide to Managing Files and Directories in C#
In this article [Show more]

    #Comprehensive Guide to Managing Files and Directories in C

    Managing files and directories is a common task in many applications. In C#, the System.IO namespace provides powerful classes to handle these tasks efficiently. This article covers various aspects of file and directory management with simple and clear C# examples using top-level statements introduced in C# 6 and later.

    Get List of Files in Directory and Subdirectories in C#

    To retrieve all files in a directory and its subdirectories, use the Directory.GetFiles method with the SearchOption.AllDirectories option.

    Example:

     

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string[] allFiles = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
    
    foreach (var file in allFiles)
    {
        Console.WriteLine(file);
    }
    

    Get List of Files in Directory with Extension in C# 

    If you need to list files with a specific extension, you can specify the extension in the search pattern.

    Example:

     

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string extension = "*.txt";
    string[] filesWithExtension = Directory.GetFiles(directoryPath, extension);
    
    foreach (var file in filesWithExtension)
    {
        Console.WriteLine(file);
    }
    

     

    Directory.GetFiles Search Pattern

    The Directory.GetFiles method supports search patterns to filter files based on name, extension, or other criteria.

    Example:

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string searchPattern = "*.jpg";
    string[] files = Directory.GetFiles(directoryPath, searchPattern);
    
    foreach (var file in files)
    {
        Console.WriteLine(file);
    }
    

     

    Find File by Name

    To find a specific file by name, you can use Directory.GetFiles with a search pattern.

    Example:

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string fileName = "example.txt";
    string[] files = Directory.GetFiles(directoryPath, fileName);
    
    if (files.Length > 0)
    {
        Console.WriteLine("File found: " + files[0]);
    }
    else
    {
        Console.WriteLine("File not found.");
    }
    

    Get Directory from Path

    To get the directory from a file path, you can use Path.GetDirectoryName.

    Example:

    using System;
    using System.IO;
    
    string filePath = @"C:\ExampleDirectory\example.txt";
    string directory = Path.GetDirectoryName(filePath);
    
    Console.WriteLine("Directory: " + directory);
    

    Directory GetFiles Search Pattern FileName

    If you need to search for files matching a specific filename pattern, use Directory.GetFiles with the pattern.

    :Example

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string searchPattern = "file*.txt";
    string[] files = Directory.GetFiles(directoryPath, searchPattern);
    
    foreach (var file in files)
    {
        Console.WriteLine(file);
    }
    

    Folder Copy All Files in C# 

    To copy all files from one folder to another, you can use File.Copy in a loop.

    Example:

    
    using System;
    using System.IO;
    
    string sourceDirectory = @"C:\SourceDirectory";
    string destinationDirectory = @"C:\DestinationDirectory";
    
    foreach (var file in Directory.GetFiles(sourceDirectory))
    {
        string fileName = Path.GetFileName(file);
        string destFile = Path.Combine(destinationDirectory, fileName);
        File.Copy(file, destFile, true); // true to overwrite existing files
    }

    List Folders in Directory

    To list all folders in a directory, use Directory.GetDirectories.

    Example:

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string[] folders = Directory.GetDirectories(directoryPath);
    
    foreach (var folder in folders)
    {
        Console.WriteLine(folder);
    }
    

    Open a Folder in C# 

    To open a folder in Windows Explorer, you can use the Process class.

    Example:

    using System;
    using System.Diagnostics;
    
    string folderPath = @"C:\ExampleDirectory";
    Process.Start("explorer.exe", folderPath);
    

    Get Folders in Directory

    Similar to listing folders, Directory.GetDirectories can be used to get folders in a directory.

    Example:

    using System;
    using System.IO;
    
    string directoryPath = @"C:\ExampleDirectory";
    string[] folders = Directory.GetDirectories(directoryPath);
    
    foreach (var folder in folders)
    {
        Console.WriteLine(folder);
    }
    

    Folder Picker in C# 

    For a folder picker dialog, you typically use Windows Forms or WPF. Here's a basic example using Windows Forms.

    :Example

     

    using System;
    using System.Windows.Forms;
    
    class Program
    {
        [STAThread]
        static void Main()
        {
            using (var folderDialog = new FolderBrowserDialog())
            {
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    Console.WriteLine("Selected folder: " + folderDialog.SelectedPath);
                }
            }
        }
    }
    

     

     

     

    Author Information
    • Author: Ehsan Babaei

    Send Comment



    Comments