Reputation: 1171
I want to use FileStream
instead StreamWriter
, because FileStream
has .Flush(true)
witch writes cached data on stream into disk, but when I changed StreamWriter
to FileStream
, there are many errors in my code, because FileStream
does not have .Write()
and .WriteLine()
methods
How can I use FileStream
Like StreamWriter
? I want call .WriteLine(string _)
on FileStream
but it's not exists. Is there any way to add this method?
Upvotes: 0
Views: 681
Reputation: 1171
It's possible by extension methods like this
using System.IO;
...
public static class MyExtensionClass
{
public static void Write(this FileStream fs, object value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value + "");
fs.Write(info, 0, info.Length);
}
public static void WriteLine(this FileStream fs, object value = null)
{
fs.Write(value + "\r\n");
}
}
Upvotes: 1