List of images as HTML Learn programming C# Sharp

Lesson:

Additional Libraries


Exercise:

List of images as HTML 41


Objetive:

Create a program to create an HTML file containing the list of images (PNG and JPG) in the current directory.

For example, in the current directory there are images called.

1.png
2.jpg


Code:

using System;
using System.IO;
using System.Collections.Generic;
class ListImagesHTML
{
    static void Main()
    {
        CreateHtml(GetImages());
    }

    static void CreateHtml(List listImages)
    {
        try
        {
            StreamWriter writer = new StreamWriter(File.Create("images.html"));

            writer.WriteLine("");
            writer.WriteLine("");

            foreach (string image in listImages)
            {
                writer.WriteLine("" + image + "");
                writer.WriteLine("\");
            }

            writer.WriteLine("");
            writer.WriteLine("");

            writer.Close();
        }
        catch
        {
            Console.WriteLine("Error writing html.");
        }
    }

    static List GetImages()
    {
        List ListImages = new List();

        string[] files = Directory.GetFiles(".");

        foreach (string file in files)
        {
            string extension = Path.GetExtension(file);

            switch (extension)
            {
                case ".png":
                case ".jpg":
                case ".jpge":
                    ListImages.Add(file.Substring(2));
                    break;
            }
        }

        return ListImages;
    }
}