Saturday, July 10, 2021

Create HTML File in Console Application using C#,VB.Net

Create HTML File in Console Application using C#,VB.Net

In this article I will explain with an example, how to create HTML File in Console (Command) Application using C# and VB.Net.

First, an HTML Table will be generated in the form of an HTML string and then the HTML string will be written to an HTML File and saved in Folder (Directory) using C# and VB.Net.

Namespaces

You will need to import the following namespace.

C#

using System.IO;

 VB.Net

Imports System.IO

Creating HTML File in Console Application using C# and VB.Net

Inside the Main method, the captured value of Name is wrapped into an HTML Table string and then using the WriteAllText method of the File class, the HTML string is written to an HTML File and finally, the HTML File is saved in a Folder (Directory).

C#

static void Main(string[] args)

{

    Console.WriteLine("Enter Name:");

    string name = Console.ReadLine();

 

    //Table start.

    string html = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:arial'>";

 

    //Adding HeaderRow.

    html += "<tr>";

    html += "<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Name</th>";

    html += "</tr>";

 

    //Adding DataRow.

    html += "<tr>";

    html += "<td style='width:120px;border: 1px solid #ccc'>" + name + "</td>";

    html += "</tr>";

 

    //Table end.

    html += "</table>";

 

    //Create the HTML file.

    File.WriteAllText(@"E:\Files\Console.htm", html);

 

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

    Console.ReadLine();

}

VB.Net

Sub Main()

    Console.WriteLine("Enter Name:")

    Dim name As String = Console.ReadLine()

 

    'Table start.

    Dim html As String = "<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:arial'>"

 

    'Adding HeaderRow.

    html &= "<tr>"

    html &= "<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Name</th>"

    html &= "</tr>"

    'Adding DataRow.

    html &= "<tr>"

    html &= "<td style='width:120px;border: 1px solid #ccc'>" & name & "</td>"

    html &= "</tr>"

    'Table end.

    html &= "</table>"

    'Create the HTML file.

    File.WriteAllText("E:\Files\Console.htm", html)

 

    Console.WriteLine("HTML File created.")

    Console.ReadLine()

End Sub

No comments:

Post a Comment

DotNet Latest Technologies

  I'm not sure exactly what you are looking for or how "recent" the technologies that you might wan to look into are, but I...