0

I am writing a program that needs to download an .exe file from a website and then save it to the hard drive. After many web searches and fumbling through examples here is the code I have come up with so far:

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(link to my.exe);
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
        Stream responseStream = webResponse.GetResponseStream();               
        string tempFilePath = someLocation;
        byte[] buffer = new byte[1024];
        using (FileStream fileStream = File.Create(tempFilePath))
        {
            int size = responseStream.Read(buffer, 0, buffer.Length);
            while (size > 0)
            {
                fileStream.Write(buffer, 0, size);
                size = responseStream.Read(buffer, 0, buffer.Length);
            }
        }
        fileStream.Flush();
        fileStream.Close();

I keep seeing the following error:

Exception: System.NotSupportedException: This stream does not support seek operations.     at System.Net.ConnectStream.get_Length()    

Any suggestions ??

Ahsan
  • 2,964
  • 11
  • 53
  • 96

1 Answers1

-1

try this:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(link to my.exe);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();               
string tempFilePath = someLocation;
byte[] buffer = new byte[1024];
using (FileStream fileStream = File.Create(tempFilePath))
{
     responseStream.CopyTo(fileStream);
}
fileStream.Flush();
fileStream.Close();