C# Exercise: 216 Display directory

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.

 Exercise

Display directory

 Objetive

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

 Example Code

// 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
        }
    }
}

More C# Exercises of Additional Libraries

 Date and time
Create a program to display the current date and time with the following format: Today is 6 of February of 2015. It´s 03:23:12...
 Display executable files in directory
Create a program that shows the names (excluding the path) of all executable files (.com, .exe, .bat, .cmd) in the current folder....
 Date and time continuous
Create a program that shows the current time in the top-right corner of the screen with the format: 12:52:03 The program should pause for one se...
 Sitemap creator
A "sitemap" is a file that webmasters can use to inform Google about the webpages that their website includes, with the aim of achieving better search...
 List of images as HTML
Create a program that creates an HTML file that lists all the images (PNG and JPG) in the current folder. For instance, if the current folder conta...
 System information
Create a program that shows specific system details, including the computer name, domain name, and the username of the user who is currently logged in...
 Sitemap creator v2
You need to create a program that takes the following parameters: the name of a text file containing the URLs, the modification date, and the change f...
 Surf directory
Create a program that displays the files in the current folder and allows the user to move up and down the list. If the user presses Enter on a folder...
 Subdirectories
Create a program to store the files that are located in a particular folder and its subfolders. Then, it will ask the user which text to search and...