Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Well, I'm testing a form where there is a streamwriter concept to write data in a text file. I don't have problem with it, except when debugging, if I fill the form when testing more than 4 or 5 times, it stops and highlighted in yellow colour the entire line of the streamwriter code saying to me:

IOExeption was unhandled
The process cannot access the file 'C:\Test.txt' because it is being used by another process.

I don't know why it is saying this when filling more than 5 times the form when debugging. The file is not opened anywhere, it is just in the Local Drive C.
Posted
Updated 26-Apr-11 21:58pm
v2

The file cannot be opened because it is still locked, probably by your previously used stream writer. Your page tries to open the file again, but the StreamWriter which has been used before has not yet been deleted by the garbage collection ansd still holds a lock on the file.

You should do the following things:

1) Catch and handle exceptions in this function. Unforseeable errors can always happen when accessing files and you do not want your website to crash just because of that.

2) Add a finally block where you close the StreamWriter, no matter what has happened. After that you must call its Dispose() method to release any resources (and file locks) without waiting for the garbage collection.
 
Share this answer
 
Comments
555336 27-Apr-11 4:50am    
How to do that? I'm new in Vb.net Here is my full code of it: Dim textfilestream As System.IO.TextWriter textfilestream = System.IO.File.AppendText("C:\Test.txt") textfilestream.WriteLine("xxxx") textfilestream.Flush() MessageBox.Show("You saved your work!")
[no name] 27-Apr-11 5:16am    
Just search for 'try', 'catch' and 'finally' in the MSDN. Those are VB keywords which belong together and are used for error handling. The 'try' block encloses the code that may fail. In your case that would be the lines which open the file and write to it. The 'catch' block is executed only when an error occurs (in form of an exception). The Exception is caught there and does not cause your page to crash. Here you can handle the error if it is correctable or at least report it to the user. The 'finally' block contains code that must be executed, no matter what happened. Here you must put the code to clean up. This means closing the StreamWriter and then calling its Dispose() method.
555336 27-Apr-11 5:22am    
i got the answer on MSDN. I just add Using and End using with and remove the DIM :) finally it works. Thank you all. Your answers also are good when I was searching.
[no name] 27-Apr-11 5:34am    
Great. You now have improved your code so that the StreamWriter is managed in a better fashion and this error is now avoided. But take a look at the 'try', 'catch' and 'finally' stuff. File IO without being prepared for handling errors generally is no good idea. Also, take a look at the Dispose() method of the strem writer. Dispose() is used to relase system resources before the garbage collection finally does so. Many objects in the framework have a Dispose() method for this purpose and you will run into similar problems again if you don't know what this is about.
555336 27-Apr-11 6:10am    
I did this:

Using textfilestream As System.IO.TextWriter = System.IO.File.AppendText("C:\test.txt")
textfilestream.WriteLine("xxx")
textfilestream.Flush()
MessageBox.Show("xx")
End Using
The error message is telling you what is happening, the file is locked. I would make sure that you


1. Dont have any errors while writing to the file
2. Releasing the file after writing to it.
 
Share this answer
 
Comments
555336 27-Apr-11 4:02am    
What you mean?
One reason could be : You have opened the file using StreamWriter and before calling StreamWriter.Close() you have exited the application.

Make sure you close the StreamWriter before exiting the application.
Hope this helps.
All the best.
 
Share this answer
 
Comments
555336 27-Apr-11 4:18am    
here is the code:

Dim textfilestream As System.IO.TextWriter
textfilestream = System.IO.File.AppendText("C:Test.txt")
textfilestream.WriteLine("xxxxx")
textfilestream.Flush()
Pravin Patil, Mumbai 28-Apr-11 5:35am    
textfilestream.close() ....?

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