textwriter.close deletes my data
hey,
i got this weird problem.
i am making a little textediting program and somehow when i have written a txt file and close it via:
tc.Close();
the text file goes blank. nothing left.
the code ( sorry if it looks like rubbish. i just started programming in C#) :
Code:
static void opentxtfile(string input)
{
Console.Clear();
if (input == " ")
{
Console.WriteLine("TEXTEDITOR \n \nGive the name of the text file, or 1 to exit\n\n");
Console.Write("file: ");
string input2 = Console.ReadLine();
Thread.Sleep(1);
if (input2 == "1" )
{
txtedit();
return;
}
else
opentxtfile(input2);
return;
}
else
Thread.Sleep(1);
if (!File.Exists(MAINDIR + "\\documents\\text\\" + input + ".txt"))
{
Console.WriteLine("text file does not exist.");
txtedit();
return;
}
TextReader tw = new StreamReader(MAINDIR + "\\documents\\text\\" + input + ".txt");
Int32 NumberOfLines = 1;
Thread.Sleep(1);
using (StreamReader r = new StreamReader(MAINDIR + "\\documents\\text\\" + input + ".txt"))
{
int i = 0;
while (r.ReadLine() != null) { i++; }
NumberOfLines = i + 1;
}
string[] ListLines = new string[NumberOfLines+1];
for (int i = 1; i < NumberOfLines; i++)
{
ListLines[i] = tw.ReadLine();
}
Console.Clear();
for (int y = 1; y < NumberOfLines; y++)
{
Console.WriteLine(ListLines[y]);
}
tw.Close();
Thread.Sleep(1);
string newline = Console.ReadLine();
TextWriter tc = new StreamWriter (MAINDIR + "\\documents\\text\\" + input + ".txt");
if (newline != "/quit")
{
Thread.Sleep(5);
int a;
for (a = 1; a < NumberOfLines; a++)
{
tc.WriteLine(ListLines[a]);
}
Thread.Sleep(5);
ListLines[NumberOfLines] = newline;
tc.WriteLine(ListLines[NumberOfLines]);
tc.Close();
Thread.Sleep(5);
opentxtfile(input);
return;
}
else
tc.Close(); // right here
Thread.Sleep(500);
txtedit();
return;
}