Mostrar archivos ejecutables en el directorio Ejercicio C# - Curso de Programación C# (C Sharp)

 Ejercicio

Mostrar archivos ejecutables en el directorio

 Objetivo

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

 Código de Ejemplo

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

Más ejercicios C# Sharp de Bibliotecas Adicionales


Juan A. Ripoll - Tutoriales y Cursos de Programacion© 2025 Todos los derechos reservados.  Condiciones legales.