New Stream.CopyTo() in .Net framework 4

(This post is part of my Visual Studio 2010 series)

Stream.CopyTo() is a new method added to System.IO.Stream in .Net framework 4, which allows you to copy bytes from one stream to another easily and makes your code simple.

 

FileStream sourceStream = new FileStream(@"G:\test\art.bmp", FileMode.Open);
FileStream targetStream = new FileStream(@"G:\test\art-copy.bmp", FileMode.CreateNew);

sourceStream.CopyTo(targetStream);

targetStream.Close();
sourceStream.Close();

 

This method is also available in MemoryStream(any subclass of System.IO.Stream).

This is a notable method if you especially do more file IO in your application, you may also look at your old code for chances to refactor it with this method(if you have time :)

Bookmark / Share

Comments for this entry..

New Stream.CopyTo() in .Net framework 4

New Stream.CopyTo() in .Net framework 4

by.. CodeHappiness.com | July 17, 2010 21:36 | Reply

.NET 4.0 New Feature - Stream.CopyTo

.NET 4.0 New Feature - Stream.CopyTo

by.. Level Up | November 24, 2010 10:56 | Reply

Write your comment..




biuquote
  • Comment
  • Preview
Loading