There are at least 2 options based on how big your text file is:
If the text file is only supposed to be a few lines, use this solution:
importFile = File.ReadAllText(fileName).Split('\n'); StringBuilder newContents = new StringBuilder(); foreach (string line in importFile) { data = line.Split(','); userName = data[0]; // "Ben" password = data[1]; // "welcome1" if (data[0] == UModel.UserName && UModel.UserPassword == data[1]) { line = data[0] + "," + UModel.ConfirmPassword + "," + data[2]; newContents.Append(line); newContents.AppendLine(); } } File.WriteAllText(fileName, newContents.ToString());
If the text file is extremely huge, then you need to make use of 2 files.
Read line by line from one file, write it to another temp file line by line, till you find the matching line at which point you will write the modified line to the new file and then continue writing all the remaining lines as-is from the first file. Finally, you need to delete the old file and rename the new one to the old one.