1

I need to use .NetFramework3.5 for my application, but CopyTo() and WriteTo() methods are not available in 3.5. what are the equivalent methods in 3.5?

when I run the code with 3.5 it is throwing the following error:

'System.IO.Stream' does not contain a definition for 'WriteTo' and no extension method 'WriteTo' accepting a first argument of type 'System.IO.Stream' could be found

Here is the code:

int fileId = 1;
foreach (string uri in uriList)
{

    request = (HttpWebRequest)WebRequest.Create (baseURL + uri);
    request.Headers.Add ("X", authenticateStr);
    request.Accept = "application/pdf";
    request.Method = "GET";

    webResponse = (HttpWebResponse)request.GetResponse();
    using (MemoryStream ms = new MemoryStream())
    using (FileStream outfile = new FileStream("document_", FileMode.Create)) {
        webResponse.GetResponseStream().WriteTo(ms);
        if (ms.Length > int.MaxValue) {
            throw new NotSupportedException("Cannot write a file larger than 2GB.");
        }
        outfile.Write(ms.GetBuffer(), 0, (int)ms.Length);
    }
}
Console.WriteLine("Done!");
Richard
  • 106,783
  • 21
  • 203
  • 265
M.K
  • 51
  • 1
  • 8
  • From MSDN - "When the current stream is open, this method is equivalent to calling Stream.Write on the underlying buffer of this stream." So just create a extension method. – weismat Apr 23 '13 at 08:15
  • Try to find out your solution from below link [Link1][1] [Link2][2] [1]: http://stackoverflow.com/questions/3985795/gzipstream-copyto-alternate-and-easy-way-in-net-3-5 [2]: http://stackoverflow.com/questions/5730863/how-to-use-stream-copyto-on-framework-3-5 – Amit Apr 23 '13 at 08:16
  • How to use the extension method in my code? Now I' m using webResponse.GetResponseStream().WriteTo(ms); what are the modifications to be done to the above code? – M.K Apr 23 '13 at 09:31
  • I created the extension method, how to use this in the code? – M.K Apr 23 '13 at 10:25

2 Answers2

2
  1. Stream.CopyTo was indeed added with .NET 4. Earlier versions of .NET are missing lots of helpful methods added in latter versions. .NET 4.5 continues to have lots of "obvious" method missing, I assume future versions will continue to add such helpers if MS perceive sufficient demand.

  2. There is no Stream.WriteTo. It only exists on some subclasses (eg. MemoryStream.WriteTo which has existed since .NET 1.0).

(I suspect Stream.CopyTo was added as a common MemoryStream.WriteTo but obviously using WriteTo would be an API breaking change because, for example, reflection on it would give different results.)

Richard
  • 106,783
  • 21
  • 203
  • 265
0

If you need for CopyTo I use this extension

public static void CopyTo(this Stream input, Stream output)
{
   // This method exists only in .NET 4 and higher

   byte[] buffer = new byte[4 * 1024];
   int bytesRead;

   while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
   {
      output.Write(buffer, 0, bytesRead);
   }
}
Milan Matějka
  • 2,654
  • 1
  • 21
  • 23