{"id":131,"date":"2010-03-20T12:58:00","date_gmt":"2010-03-20T07:28:00","guid":{"rendered":"https:\/\/ashokaon.tech\/blog\/?p=131"},"modified":"2020-07-18T01:25:09","modified_gmt":"2020-07-17T19:55:09","slug":"stream-copyto","status":"publish","type":"post","link":"https:\/\/ashokaon.tech\/blog\/stream-copyto\/","title":{"rendered":"New Stream.CopyTo() in .Net framework 4"},"content":{"rendered":"<p><span style=\"font-size: 10px;background-color:#FFFF99;display:block\">(Note: This blog post was originally published under my old domain(codesmiles.com), <a href=\"http:\/\/codesmiles.com\/post\/stream-copyto.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. <a href=\"https:\/\/web.archive.org\/web\/2010*\/http:\/\/www.codehappiness.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Web Archive link<\/a>.<\/span><\/p>\n<p style=\"color: #777; font-size: 11px\">(This post is part of my <a href=\"http:\/\/www.codesmiles.com\/post\/visual-studio-2010-new-features.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">Visual Studio 2010 series<\/a>)<\/p>\n<p>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.[more]<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: c-sharp;\">FileStream sourceStream = new FileStream(@\"G:\\test\\art.bmp\", FileMode.Open);\r\nFileStream targetStream = new FileStream(@\"G:\\test\\art-copy.bmp\", FileMode.CreateNew);\r\n\r\nsourceStream.CopyTo(targetStream);\r\n\r\ntargetStream.Close();\r\nsourceStream.Close();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>This method is also available in MemoryStream(any subclass of System.IO.Stream).<\/p>\n<p>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 \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<div class=\"pflink\">\n<ul>\n<li><a title=\"Click to Subscribe\" href=\"http:\/\/feeds.feedburner.com\/codehappiness\"> <img decoding=\"async\" class=\"pflinkfeed\" src=\"http:\/\/www.codesmiles.com\/pics\/feed-icon-16x16.png\" alt=\"RSS feed\"><\/a>&nbsp;<span><a title=\"Click to Subscribe\" href=\"http:\/\/feeds.feedburner.com\/codehappiness\">Subscribe to the feed!<\/a> (<a href=\"http:\/\/www.codesmiles.com\/post\/what-is-an-rss-feed.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">?<\/a>) and receive any such future posts on your favorite <a href=\"http:\/\/www.codesmiles.com\/post\/what-is-an-rss-feed.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">feed reader<\/a><\/span><\/li>\n<li>Any comments? <a title=\"Comments can be used to express your thoughts about this blog entry\" rel=\"nofollow\" href=\"http:\/\/www.codesmiles.com\/post\/stream-copyto.aspx#befcomment\">write here<\/a>..<\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Note: This blog post was originally published under my old domain(codesmiles.com), here. Web Archive link. (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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[33],"_links":{"self":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts\/131"}],"collection":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":3,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/posts\/131\/revisions\/316"}],"wp:attachment":[{"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ashokaon.tech\/blog\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}