Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

streamreader, streamwriter problem

Status
Not open for further replies.

barryna

Programmer
Feb 15, 2002
111
US
ok, this should be simple, but is not working for me. When my smart app opens, it reads a text file using streamreader. I then close the file. When the app is closed, I am opening the file using streamwriter to re-output my settings. The problem I am having is that if the file was opened by the app earlier, I get an error trying to re-open it later for writing to it. If I don't open it at the beginning, it is able to write to it just fine. I am setting file.close() and then file=nothing after reading it in my form_load, and that doesn't help. The error is IOExceoption, very general.

Nick
 
k, figured it out. An error was being thrown up in reading the file, causeing it to skip the file.close(). That solved the problem. Not use to cariage returns and line feeds not read in.

 
barryna -
Use a try..catch..finally block to make sure you close the stream in case of an error. You don't want to leave streams open, as it may take the garbage collector 30 seconds or more to get around to freeing the reference. In the mean time you won't be able to access the file (you get a "file in use" exception).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
good idea. I will put my file.close() in the Finally to ensure it gets closed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top