Reputation: 956
I'm using .net core 1.1, previously when I was with .net framework, I usually call Close()
on FileStream
or any Stream after I finished the stream operations, but the FileStream
class in .net core 1.1 doesn't have Close method, I found Dispose()
but don't know if it's the equivalent. Anyone care to let me know the right way to correctly close with the new FileStream/StreamWriter
class in .net core?
Upvotes: 7
Views: 4507
Reputation: 98
Running into this issue, trying to keep legacy code intact, another option would be to write an extension method.
public static class FileStreamExtension
{
public static void Close(this FileStream reader)
{
reader.Dispose();
}
}
Upvotes: 0
Reputation: 1
Use using or build your own Dispose Pattern.
using (StreamWriter sw = new StreamWriter(path)
{
}
Upvotes: 0
Reputation: 2216
Implementing IDisposable
means that you can use a using
statement, which will implicitly call the Dispose()
method, thus closing the stream.
Use
using (StreamWriter sw = new StreamWriter(path))
{
// your logic here
} // here Dispose() is called implicitly and the stream is closed
Upvotes: 6