Create HTML File In Console App Using C#

In this article, I’m showing that how to create an HTML file in Console Application using c# with the example. In this demo, I’m just creating a simple HTML that shows the first and last name entered by the user.

Here is The code:

using System;
using System.IO;

namespace HtmlFileInConApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter First Name:");
            string Fname = Console.ReadLine();
            Console.WriteLine("Enter Last Name:");
            string Lname = Console.ReadLine();

            //Html Code
            string html = "<h3 style='color:#0000ff;'>First Name: </h3> <span>" + Fname + "</span>";
            html += "<h3 style='color:#0000ff;'>Last Name: </h3> <span>" + Lname + "</span>";

            File.WriteAllText(@"D:\Files\HtmlFileUsingConsoleApp.htm", html);

            Console.WriteLine("HTML File created.");
            Console.ReadLine();
        }
    }
}

Final Output:

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories