Write file from assembly resource stream to disk

I'm not sure why you're using BinaryReader/BinaryWriter at all. Personally I'd start off with a useful utility method:

public static void CopyStream(Stream input, Stream output)
{
    // Insert null checking here for production
    byte[] buffer = new byte[8192];

    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}

then call it:

using (Stream input = assembly.GetManifestResourceStream(resourceName))
using (Stream output = File.Create(path))
{
    CopyStream(input, output);
}

You can change the buffer size of course, or have it as a parameter to the method - but the main point is that this is simpler code. Is it more efficient? Nope. Are you sure you really need this code to be more efficient? Do you actually have hundreds of megabytes you need to write out to disk?

I find I rarely need code to be ultra-efficient, but I almost always need it to be simple. The sort of difference in performance that you might see between this and a "clever" approach (if one is even available) isn't likely to be a complexity-changing effect (e.g. O(n) to O(log n)) - and that's the type of performance gain which really can be worth chasing.

EDIT: As noted in comments, .NET 4.0 has Stream.CopyTo so you don't need to code this up yourself.


If the resource (file) is binary.

File.WriteAllBytes("C:\ResourceName", Resources.ResourceName);

And if the resource (file) is text.

 File.WriteAllText("C:\ResourceName", Resources.ResourceName);

Tags:

C#

.Net