このコードは、Students.txt に行を追加しますが、コードをコンパイルして実行するたびに、再度実行されます。新しいレコードが一度だけ追加されるようにするには、どのコードを追加できますか?
string newLastName = "'Constant";
string newRecord = "(LIST (LIST 'Constant 'Malachi 'D ) '1234567890 'mdconstant@mail.usi.edu 4.000000 )";
string line;
string lastName;
bool insertionPointFound = false;
for (int i = 0; i < lines.Count && !insertionPointFound; i++)
{
line = lines[i];
if (line.StartsWith("(LIST (LIST "))
{
values = line.Split(" ".ToCharArray());
lastName = values[2];
if (newLastName.CompareTo(lastName) < 0)
{
lines.Insert(i, newRecord);
insertionPointFound = true;
}
}
}
if (!insertionPointFound)
{
lines.Add(newRecord); //This record is always added, making the file longer over time
//if it is not deleted each time from the Students.txt file in
} //the bin folder.
File.WriteAllLines("Students.txt", lines);