Mike-Ward.Net

File.WriteAllBytes

The .NET runtime has so many features it is difficult to keep track. Just the other night, I need to write a byte[] to a file. I started out with something like this:

using (FileStream stream = newFileStream("myfile", FileMode.CreateNew))
{
    stream.Write(buffer, 0, buffer.Length);
}

Not bad but it is a little tedious. Then I discovered quite by accident that .NET 2.0 added helper methods to the System.File class including System.File.WriteAllBytes.

Other “helper” methods include ReadAllBytes, ReadAllLines, ReadAllText, WriteAllLines, WriteAllText and AppendAllText. These methods are not hard to write as evidenced above, but they sure are handy none-the-less.

So now the example above becomes:

File.WriteAllBytes("myfile", buffer);
← newer older →
.Net, Technology, Life, Whatever

Recent Posts

VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
Tweetz 1.1
Tweetz Desktop 1.0
Tweetz 0.11 Released
More... (1073)

Donate with PayPal