Exercise
Display Directory
Objective
Create a program that shows the files in the current folder.
Write Your C# Exercise
C# Exercise Example
// 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
}
}
}
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#.
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...
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 ...
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 "...
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...
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...
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 ...