I'm trying to create a code that asks for a user name and saves it in a text file. The problem is when I close the console app and try to run it again, it deletes all the information that was in the text file. Can anyone help me solve this problem?
public static void UserWriter(StreamWriter WriteNewLine)
{
bool EndOfWrite = true;
char Answer;
while (EndOfWrite)
{
Console.WriteLine("eneter a user name");
WriteNewLine.WriteLine(Console.ReadLine());
Console.WriteLine("did you finish ? Y/N");
Wrong: Answer = Convert.ToChar(Console.ReadLine());
switch (Answer)
{
case 'Y':
EndOfWrite = false;
break;
case 'N':
break;
default:
goto Wrong;
}
}
WriteNewLine.Close();
}
static void Main(string[] args)
{
string fileLoction = @"C:\project\user.txt";
StreamWriter WriteNewLine = new StreamWriter(fileLoction);
if (File.Exists(fileLoction))
{
WriteNewLine.Close();
WriteNewLine = File.AppendText(fileLoction);
UserWriter(WriteNewLine);
WriteNewLine.Close();
}
else
{
UserWriter(WriteNewLine);
WriteNewLine.Close();
}
}