No overwriting by streamwriter
//Hello there, these commented lines are not executing. help me out here. when i run 1st time, a file is created. when i run it second time, it dsnt get //overwritten. plz help me out in this....
namespace File_Experiment
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw;
StreamReader sr;
FileStream fs;
char c;
string pth;
pth = Directory.GetCurrentDirectory();
Console.WriteLine(pth);
pth = pth + "\\" + "textfile.txt";
if ((File.Exists(pth)))
{
Console.WriteLine("The file already exists. Append? y or n");
c = Convert.ToChar( Console.Read());
switch (c)
{
case 'y':
FileInfo fs2;
StreamWriter sw2;
fs2 = new FileInfo(pth);
sw2 = new StreamWriter(pth);
Console.WriteLine("Enter the text to be written in the file");
string line = "hello chaman";
line = Console.ReadLine(); //Why isnt this statement executing??
sw2.WriteLine(line); //Why isnt this statement executing??
Console.WriteLine("The text has been written");
break;
case 'n': break;
default: Console.WriteLine("No identifiable key inserted. The program will now exit");
break;
}
}
else
{
fs = new FileStream(pth, FileMode.Create);
sw = new StreamWriter(fs);
Console.WriteLine("Enter the text to be written in the file");
string line = Console.ReadLine();
sw.WriteLine(line);
Console.WriteLine("The text has been written");
sw.Close(); fs.Close();
}
Console.WriteLine("Thanks for using my program :)");
}
}
}