What is the proper way to close and/or dispose a StreamWriter
What is the proper way to close and/or dispose a StreamWriter
I have a program that uses StreamWriter to write a text file.
When I do this, a file is created in the Debug folder for my project:
Dim TextWrtr As StreamWriter
TextWrtr = System.IO.File.CreateText(outputFilename)This file is blank (0 KB) and has the same name as 'outputFilename'.
I've used StreamWriters many times in the past and have never encountered this before. I've always just closed it like this:
TextWrtr.Close()For some reason I can't figure out, this file doesn't want to go away. I've even tried adding:
TextWrtr.Dispose()I've also tried to just straight up delete it at the end of the program but I get an error that says the file is being used.
What am I missing? What is the proper way to close/dispose a StreamWriter??
0Sign in to vote Put it in a Using block ... that will do it for you.
Put it in a Using block ... that will do it for you.