using
System;
using
System.IO;
using
System.Collections;
namespace
InsertLineInTextFile
{
class
Program
{
static
void
Main
(string[] args)
{
string strTextFileName = "file.txt";
int iInsertAtLineNumber = 2;
string strTextToInsert = "Amudha";
ArrayList lines = newArrayList();
StreamReader rdr = newStreamReader(
strTextFileName);
string line;
while ((line = rdr.ReadLine()) != null)
lines.Add(line);
rdr.Close();
if (lines.Count > iInsertAtLineNumber)
lines.Insert(iInsertAtLineNumber,
strTextToInsert);
else
lines.Add(strTextToInsert);
StreamWriter wrtr = newStreamWriter(
strTextFileName);
foreach (string strNewLine in lines)
wrtr.WriteLine(strNewLine);
wrtr.Close();
}
}
}