cannot access file being used by another process error!

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am getting a "Process cannot access file being used by another process" when trying to save data to a text file. below is the streamwriter code.

    using (StreamWriter sw = new StreamWriter(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]", true)) 
                {
                    sw.WriteLine(dateTimePicker1.Value + "," + txtBreakFast.Text + "," + txtLunch.Text + "," + txtDinner.Text + "," + txtBedTime.Text + "," + GlobalClass.Note);
                    sw.Close();
                }


Not sure why ..I call sw.Close();
I thought when calling the using statement you don't need to flush or close the file...the using is supposed to do this ?

InkedGFX
 
Indeed you do not need the Close call in that code. If the file is opened at the 'using' statement then it will be closed at the end of the block. If the file is open when you hit the 'using' statement then you must be opening it elsewhere.
 
not sure what is going on here...I know I am not opening the file anywhere else.....this is the only code I a have to save the file being written.....

InkedGFX
 
If you close VS, reopen it and run that code, does it successfully open the file? Assuming that it does, if you get rid of the Close call and then try to open the file immediately after the 'using' block, does it succeed again?
 
the funny thing is.....when I run in Debug mode inside VS it works fine.....when I compile and try to run ...I get the error when trying to save the test results or Appointments to file......

InkedGFX
 
Back
Top Bottom