The
StreamWriter
class
does have a constructor which takes an
append
parameter:
public StreamWriter (string path, bool append);
If you don't pass the parameter, it defaults to
false
, so you will end up overwriting the file instead of appending to it.
However, that method is doing a lot of unnecessary work, since it seems all you want to do is update one line in the file:
public void ReWriteToFile(string name, string value)
{
const string FilePath = @"C:\BackgammonTxt\AppDataFile.txt";
if (System.IO.File.Exists(FilePath))
{
bool found = false;
string[] lines = System.IO.File.ReadAllLines(FilePath);
for (int index = 0; index < lines.Length; index++)
{
if (lines[index].StartsWith(name))
{
lines[index] = name + value;
found = true;
}
}
if (found)
{
System.IO.File.WriteAllLines(FilePath, lines);
}
else
{
}
}
else
{
}
}