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