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
C# Exercise Example
// 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($"
");
}
// 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}");
}
}
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 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 ...
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...
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...
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".
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...