Thursday, November 5, 2009

How to write a stream to a file

    public void WriteStreamToFile(Stream stream, String fileName)
    {
        int bytesRead;
        using (FileStream fileStream = File.Create(fileName))
        {
            byte[] buffer = new byte[32768];
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                fileStream.Write(buffer, 0, bytesRead);
            }
        }
    }

No comments:

Post a Comment