8. Open a text file and convert it into HTML file. (File operations/Strings)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace lrn2csharp8
{
class Program
{
static void Main( string[ ] args )
{
string file = "";
string html = "<html><head><title></title></head><body>{0}</body></htm>";
string textFile = "";
Console.WriteLine( "Input name of a file in the current directory to convert into an HTML document." );
Console.Write( ":" );
try
{
file = Console.ReadLine( );
FileStream fs = new FileStream( file, FileMode.Open, FileAccess.Read );
StreamReader sr = new StreamReader( fs );
while(!sr.EndOfStream)
{
string line = sr.ReadLine( );
if ( line.Length != 0 )
textFile += "<p>" + line + "</p>";
}
sr.Close( );
StreamWriter sw = new StreamWriter( fs.Name + ".html" );
sw.Write( string.Format( html, textFile ) );
sw.Close( );
fs.Close( );
}
catch ( Exception e )
{
Console.WriteLine( "WTF?!?" );
}
}
}
}
posted by dharh 2:14 AM Jan 30th, 2010