List Of Images As HTML - C# Programming Exercise

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 should search for image files in the directory and create an HTML file containing tags for each image found. This will help you learn how to work with files in a directory and create an HTML file dynamically using C# programming. This exercise also lets you practice handling and displaying images on a webpage easily. You will use functions like Directory.GetFiles to retrieve the images and write the HTML file using classes like StreamWriter.

 Category

Additional Libraries

 Exercise

List Of Images As HTML

 Objective

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 contains the following images:

1.png
2.jpg

 Write Your C# Exercise

// Import the necessary namespaces for working with file and string operations
using System;
using System.IO;

class ListImagesAsHTML
{
    // Main method where the program execution starts
    static void Main()
    {
        // Get all the image files (PNG and JPG) in the current directory
        string[] imageFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*")
                                      .Where(file => file.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                                                     file.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
                                      .ToArray();

        // Create or open the HTML file to write the image list
        string htmlFilePath = "image_list.html";
        using (StreamWriter writer = new StreamWriter(htmlFilePath))
        {
            // Write the basic HTML structure
            writer.WriteLine("");
            writer.WriteLine("Image List");
            writer.WriteLine("");
            writer.WriteLine("

List of Images

"); writer.WriteLine("
    "); // Loop through each image file and add it to the HTML list foreach (var imageFile in imageFiles) { // Get the file name without the full path string fileName = Path.GetFileName(imageFile); // Add an
  • element for each image with the tag writer.WriteLine($"
  • \"{fileName}\"
  • "); } // Close the list and body tags writer.WriteLine("
"); writer.WriteLine(""); writer.WriteLine(""); } // Inform the user that the HTML file has been created Console.WriteLine($"HTML file created: {htmlFilePath}"); } }

 Share this C# exercise

 More C# Programming Exercises of Additional Libraries

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#.

  •  System information

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

  •  Sitemap creator v2

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

  •  Surf directory

    In this exercise, you need to create a program that displays the files and folders in the current folder and allows the user to move up and down the list. If the user...

  •  Subdirectories

    In this exercise, you need to create a program that stores the files located in a specific folder and its subfolders. Then, the program should ask the user for a sear...

  •  Date and time

    In this exercise, you need to create a program that displays the current date and time in the following format:"Today is 6 of February of 2015. It´s 03:23:12".

  •  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...