Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 private void btnSaveSL_Click(object sender, EventArgs e)
        {
            string record = "";

            string path = txtFilePathSL.Text;

            StreamReader reader = new StreamReader(path);
            string line = reader.ReadToEnd();

            if (line.Contains(txtMemberIDSL.Text))
            {
                lblMessageSL.Text = "The Record exists" + "\n" + "Record is updated now";
                lblMessageSL.Visible = true;

                record += txtMemberIDSL.Text + "\t";
                record += txtFnameSL.Text + "\t";
                record += txtLnameSL.Text + "\t";
                record += txtDateSL.Text + "\t";
                record += txtClassesSL.Text + "\t";
                record += txtCostPerClassSL.Text + "\t";
                record += txtPaidSL.Text + "\t";
             
                    using (writer = new StreamWriter(@"c:\Myfile\studentsRecord.txt", append: false))
                    {
                        writer.WriteLine(record);
                    }
                               

            }
}


What I have tried:

I have tried tried file share didn't work
Posted
Updated 15-Jul-21 21:41pm

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:
C#
string line;
using (StreamReader reader = new StreamReader(path))
{
    line = reader.ReadToEnd();
}
Better yet, use the utility methods in the File class:
C#
string line = File.ReadAllText(path);
 
Share this answer
 
Comments
Maciej Los 16-Jul-21 6:37am    
5ed!
Mundane alfa 16-Jul-21 9:14am    
Thank You my savior
I converted your code to run as a function in a console program and had no trouble. I suspect you are locking the file somewhere else in your program. I suggest you use Process Explorer to determine which process is holding the file.

Google
find the owner of a file using processexplorer
to see the following instructions:
Identify which handle or DLL is using a file

    Open Process Explorer. Running as administrator.
    Enter the keyboard shortcut Ctrl+F. ...
    A search dialog box will open.
    Type in the name of the locked file or other file of interest. ...
    Click the button "Search".
    A list will be generated.


You can download Process Explorer from many sources. For example:
Process Explorer - Windows Sysinternals | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Mundane alfa 16-Jul-21 0:41am    
I just tried it is still showing the same problem
Richard Deeming 16-Jul-21 3:37am    
I've replaced your unofficial link for Process Explorer with the official Microsoft link, since many downloads from unofficial sites bundle unwanted extra software with their installers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900