Downloading a file and holding it in memory in c#

JoeD77 used Ask the Experts™
I am trying to download a remote file and hold it in memory; I am currently working with this code (listed) with no success. Hoping someone could give me some insight, example code would be very appreciated.
static void Main(string[] args)
            byte[] downloadedData;
            downloadedData = new byte[0];
            WebRequest req = WebRequest.Create("");
            WebResponse response = req.GetResponse();
            Stream stream = response.GetResponseStream();

            byte[] buffer = new byte[1024];

            int dataLength = (int)response.ContentLength;

            MemoryStream memStream = new MemoryStream();
            while (true)
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                memStream.Write(buffer, 0, bytesRead);
                downloadedData = memStream.ToArray();


            downloadedData = memStream.ToArray();


Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

What about using the WebClient object?
I dont know about exe's though. Will give it a try


All of those deal with writing the downloaded data directly to disk. I would like to hold it in memory. Any possible solutions?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Top Expert 2006
I gave WebClient a try (in actually) and it downloaded the file you have listed. I checked the filesizes and properties and they seem to match
Code example downloads the file to c:\temp\aaa.exe

c# version would be (untested)

using System.Net

WebClient Client = new WebClient();

Now you want it in memory, so I was thinking maybe read this local file in memory. Once your done processing, delete the local file.

FileStream fs = new FileStream("c:\\temp\\aaa.exe", , FileMode.Open);
BinaryReader br = new BinaryReader();
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));

WebClient also has a Open method as shown in the link I gave. Data read via stream reader. I don't know if you fancy giving that a go.


Thanks, rockiroads

Before reading your comment I was able to successfully store the file in memory without having it touch disk with this code (listed)

Now my problem is the downloaded data is not in binary form, hence it is unexecutable

Any way to convert "line" into a binary data type?
            System.Net.WebClient Client = new WebClient();
            Stream strm = Client.OpenRead("");
            StreamReader sr = new StreamReader(strm);
            string line;
            while(sr.Peek() >= 0)
                line = sr.ReadLine();
                line += sr.ReadLine();

Open in new window

This should do the trick in one line

            var data = new WebClient().DownloadData("");

data is a byte[]

You can convert the byte[] to a stream by using

var m = new MemoryStream(data);

Hope this helps.

Regards, Jacco

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial