Ejercicio
Mostrar Archivos Ejecutables En El Directorio
Objectivo
Cree un programa para mostrar el nombre (pero no la ruta de acceso) de los archivos ejecutables (.com, .exe, .bat, .cmd) en el directorio actual
Ejemplo Ejercicio C#
Mostrar Código C#
// Import the System.IO namespace which provides methods for working with files and directories
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(); // Retrieves the current working directory
// Display the message to the user about executable files in the current directory
Console.WriteLine("Executable files in the current directory:");
// Get all executable files (.exe, .com, .bat, .cmd) in the current directory
string[] executableFiles = Directory.GetFiles(currentDirectory, "*.*") // Get all files in the directory
.Where(file => file.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) || // Check if file ends with .exe
file.EndsWith(".com", StringComparison.OrdinalIgnoreCase) || // Check if file ends with .com
file.EndsWith(".bat", StringComparison.OrdinalIgnoreCase) || // Check if file ends with .bat
file.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)) // Check if file ends with .cmd
.ToArray(); // Convert the filtered result into an array
// Loop through and display each executable file
foreach (string file in executableFiles)
{
Console.WriteLine(Path.GetFileName(file)); // Display only the file name (no path)
}
}
}
Salida
Assuming the current directory contains the following files:
program.exe
document1.txt
script.bat
image1.jpg
run.com
cleanup.cmd
When running the program, the output will be:
Executable files in the current directory:
program.exe
script.bat
run.com
cleanup.cmd
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp