Updating Records In Text Files
<p>I need to create a student management system in a C# console. I have to use a text file for saving data. I can add data and retrieve data in text files but I am unable to update any student record. How can I update specific student records in a text file? </p>

<p>For example, my program will ask users to input a student that he wants to update then the program should fetch the complete record of that student. The program again asks the user to select the field or fields that he wants to update. After updating the field, the record should be updated.</p>

<p>This is my code so far, but it doesn't seem to work:</p>

<pre><code>StringBuilder newFile = new StringBuilder();

string temp = null;
string userchoice = null;
string replacetext =null;
string update = null;

Console.WriteLine("enter the id of student to update the record");

userchoice = Console.ReadLine();

String[] file=File.ReadAllLines(@"myfile.txt");
foreach (string line in file)
{
    if(line.Contains(userchoice))
    {
        Console.WriteLine("enter the data you want to replace");
        replacetext=Console.ReadLine();

        if(line.Contains(update)){
            Console.WriteLine("enter the data you want to replace with");
            update=Console.ReadLine();
            temp=line.Replace(replacetext,update);
            newFile.Append(temp+"\r\n");
            continue;
        }
        newFile.Append(line+"\r\n");
    }
    File.WriteAllText(@"myfile.txt",newFile.ToString());
}
</code></pre>
