Display Directory - C# Programming Exercise

In this exercise, you need to create a program that shows all the files in the current folder. The program should list the files available in the directory where the program is executed.

This exercise will allow you to practice using file system functionalities in C#, such as retrieving the list of files in a directory and displaying them in the console.

You will learn how to interact with the file system, retrieve information about the files in the current directory, and present it in an organized manner in the user interface.

 Category

Additional Libraries

 Exercise

Display Directory

 Objective

Create a program that shows the files in the current folder.

 Write Your C# Exercise

// Import the System.IO namespace which provides classes for file and directory operations
using System;
using System.IO;

class Program
{
    // Main method to execute the program
    static void Main()
    {
        // Get the current directory where the program is running
        string currentDirectory = Directory.GetCurrentDirectory();  // Get the current working directory

        // Display the current directory
        Console.WriteLine("Files in the current directory:");

        // Get all files in the current directory
        string[] files = Directory.GetFiles(currentDirectory);  // Get an array of file paths in the current directory

        // Loop through and display each file
        foreach (string file in files)
        {
            Console.WriteLine(Path.GetFileName(file));  // Display just the file name, not the full path
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Additional Libraries

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Display executable files in directory

    In this exercise, you need to create a program that shows the names of all executable files (.com, .exe, .bat, .cmd) in the current folder, excluding the full path. The prog...

  •  Date and time continuous

    In this exercise, you need to create a program that displays the current time in the top-right corner of the screen with the format "HH:mm:ss". The program should pause for ...

  •  Sitemap creator

    In this exercise, you need to create a program that displays the contents of a preliminary "sitemap" on the screen. The sitemap should be generated from the list of "...

  •  List of images as HTML

    In this exercise, you need to create a program that generates an HTML file listing all the images (PNG and JPG files) in the current folder. The program...

  •  System information

    In this exercise, you need to create a program that displays specific system details, including the computer name, domain name, and the username of the current...

  •  Sitemap creator v2

    In this exercise, you need to create a program that takes three parameters: the name of a text file containing the URLs, the modification date, and the change ...