I'm using a RTF file as a template for an ASP.NET web application. My VB.NET code reads the file using a StreamReader, replaces some strings, and creates a new Word document with data from the database. My code closes and disposes of the StreamReader. However, when I attempt to upload a revised RTF file to the web server I get an error, "Cannot open the file for writing". So obviously the file remains open long after the ASP.NET page has run.
How can I force the StreamReader to close? Even editing the web.config file to force the web application to restart isn't enough to kill the lock on this file.
consider using the "using" idiom, also available in VB.net
using(StreamReader reader = new StreamReader(...)) {
}
the stream will get closed, even if exception is thrown
Consider closing all IDisposable implementations like this