List of images as HTML Learn programming C#



Lesson:

Additional Libraries


Exercise:

List of images as HTML


Objetive:

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


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;
    }
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.