At a guess,
txtFilePathSL.Text
contains
"c:\Myfile\studentsRecord.txt"
; you are trying to read from and write to the same file.
Your code opens a
StreamReader
for the path, but never closes it. You then try to open a
StreamWriter
to the same path, but the file is locked by your own code.
Change your code so that your
StreamReader
is closed before you try to write to the file:
string line;
using (StreamReader reader = new StreamReader(path))
{
line = reader.ReadToEnd();
}
Better yet, use the utility methods in the
File
class:
string line = File.ReadAllText(path);