Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

TXT to HTML translator - Practice Exercises C# Sharp

Lesson 8:

File management

Exercise 8.22:

TXT to HTML translator


Create a "Text to HTML converter", which will read a source text file and create a HTML file from its contents. For example, if the file contains:

Soy yo
Ya he terminado

The name of the destination file must be the same as the source file, but with ".html" extension (which will replace the original ".txt" extension, if it exists). The "title" in the "head" must be taken from the file name.

Source Code:

using System;
using System.IO;
namespace TXTtoHTML
    class Program
        static void Main()
            Console.Write("Enter name of file: ");
            string nameFileTxt = Console.ReadLine();
            string nameFileHtml = nameFileTxt.Substring(0, nameFileTxt.Length - 4);

            if (File.Exists(nameFileTxt))
                StreamReader myfileTxt;
                StreamWriter myfileHtml;

                myfileTxt = File.OpenText(nameFileTxt);
                myfileHtml = File.CreateText(nameFileHtml + ".html");
                string line;

                myfileHtml.WriteLine("" + nameFileHtml + "");
                    line = myfileTxt.ReadLine();
                    if (line != null)
                        myfileHtml.WriteLine("" + line + "");
                while (line != null);


Exercisey 8.22

Privacy Policy:

Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.


This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.