Downloading a file and holding it in memory in c#

JoeD77
JoeD77 used Ask the Experts™
on
Hello,
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("http://www.nsrdl.com/apps/msizap.exe");
            WebResponse response = req.GetResponse();
            Stream stream = response.GetResponseStream();

            byte[] buffer = new byte[1024];

            int dataLength = (int)response.ContentLength;
            Console.Write(dataLength);


            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();

            stream.Close();
            memStream.Close();
}

Open in new window

Comment
Watch Question

Do more with

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

Commented:
What about using the WebClient object? http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=115
I dont know about exe's though. Will give it a try

Author

Commented:
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
Commented:
I gave WebClient a try (in vb.net 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();
Client.DownloadFile("http://www.nsrdl.com/apps/msizap.exe","c:\temp\aaa.exe");
MessageBox.show("Downloaded");

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));
fs.Close();
br.Close();


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.

Author

Commented:
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("http://www.nsrdl.com/apps/msizap.exe");
            StreamReader sr = new StreamReader(strm);
            
            
            string line;
            while(sr.Peek() >= 0)
            {
                line = sr.ReadLine();
                line += sr.ReadLine();
            }
            strm.Close();

Open in new window

Commented:
This should do the trick in one line

            var data = new WebClient().DownloadData("http://www.nsrdl.com/apps/msizap.exe");

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