ColonelPanic

System.IO.Stream to System.IO.File

Apr 27th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. public static class IOExtensions
  2. {
  3.     public static void SaveToFile(this System.IO.Stream stream, string fileFullPath)
  4.     {
  5.         System.Diagnostics.Debug.Assert(string.IsNullOrEmpty(fileFullPath) == false);
  6.         if (stream.Length == 0) return;
  7.  
  8.         using (System.IO.FileStream fileStream = System.IO.File.Create(fileFullPath))
  9.         {
  10.             const int bufferSize = 0x100;
  11.             byte[] buffer = new byte[bufferSize];
  12.             int bytesRead = 0;
  13.             while ((bytesRead = stream.Read(buffer, 0, bufferSize)) > 0)
  14.             {
  15.                 fileStream.Write(buffer, 0, bytesRead);
  16.             }
  17.         }
  18.     }
  19. }
Add Comment
Please, Sign In to add comment