mrbm
mrbm

Reputation: 1171

Use FileStream Like StreamWriter (Write, WriteLine)

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 FileStreamdoes not have .Write() and .WriteLine() methods

How can I use FileStreamLike StreamWriter? I want call .WriteLine(string _) on FileStreambut it's not exists. Is there any way to add this method?

Upvotes: 0

Views: 681

Answers (1)

mrbm
mrbm

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

Related Questions