Click here to Skip to main content
14,839,738 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.
Updated 26-Apr-11 21:58pm

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.
555336 27-Apr-11 4:50am
How to do that? I'm new in 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")
End Using
[no name] 27-Apr-11 6:27am
Right, rhe textfilestream is the resource that is controlled by the 'using' block. When 'EndUsing' is reached, the Dispose() method of the textfilestream is automatically called. This is an alternative way of doing what I suggested and takes care that the error does not occur again.

The other thing is, that you are not prepared to handle exceptions. Exceptions are 'thrown' when something went wrong. If your program does not 'catch' them, the exception is called 'unhandled', which leads to an instant termination of your program or an ugly error screen on ASP.Net webpages. This is what happened to you while debugging. The debugger was so nice to show you the code line from where the exception came and all available information to the exception. You will not be able to write code which can never fail. That's why must learn how catch exceptions and somehow deal with them. Really robust programs catch any exception, correct the situation where this is possible and at least notify the user or make a log entry. Letting the program terminate because of an unhandled exception is not acceptable. I know this may be hard for a beginner, but it's better when you try to consider those things from the beginning and keep an eye on them. It will save you a lot of frustration.
555336 27-Apr-11 6:42am
But I'm a beginner and my program is very simple. Just a form to fill some fields and save to a text file that's all. But as you said, the code I implemented above is right, so I prefer to leave it like this.
[no name] 27-Apr-11 6:49am
Sure. But if the entire thing is good for something, then it is to learn a few things. In this case it is a lesson in disposing resources like files and perhaps handling errors. You now have avoided a possible source of errors, but there are many more and those things have a habit of happening when you least expect or need them :)

Just remember this exception stuff when you run into the next one. It will happen.
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.
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.
555336 27-Apr-11 4:18am
here is the code:

Dim textfilestream As System.IO.TextWriter
textfilestream = System.IO.File.AppendText("C:Test.txt")
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