0. Obtain the network IP address and port:
NameValueCollection nameValueTable = new NameValueCollection(); if (ApplicationDeployment.IsNetworkDeployed) { string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query; nameValueTable = HttpUtility.ParseQueryString(queryString); }
Namevaluetable ["ip"];
1. Obtain the network file size:
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.Method = "GET"; httpWebRequest.KeepAlive = false; httpWebRequest.AllowAutoRedirect = true; httpWebRequest.Referer = url.Substring(0, url.LastIndexOf("/") + 1); httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.2;)"; WebResponse webResponse = httpWebRequest.GetResponse(); Int64 fileSize = webResponse.ContentLength; webResponse.Close(); httpWebRequest.Abort(); httpWebRequest = null;
2. Download an object
Byte [] bytes = new byte [This. buffersize]; int32 readbytes = 0; filestream FS = new filestream (threadfilename [currentthreadindex], filemode. Create, fileaccess. Write );
Httpwebrequest = (httpwebrequest) httpwebrequest. create (URL); httpwebrequest. method = "get"; httpwebrequest. keepalive = false; httpwebrequest. allowautoredirect = true; httpwebrequest. referer = URL. substring (0, URL. lastindexof ("/") + 1); httpwebrequest. useragent = "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.2;)"; // The starting position and length of the received httpwebrequest. addrange (convert. toint32 (threadfilestart [currentthreadindex]), convert. toint32 (threadfilestart [currentthreadindex] + threadfilesize [currentthreadindex]-1); webresponse = httpwebrequest. getresponse (); stream = webresponse. getresponsestream (); // get the received stream readbytes = stream. read (bytes, 0, this. buffersize); While (readbytes> 0) {FS. write (bytes, 0, readbytes); readbytes = stream. read (bytes, 0, this. buffersize);} FS. flush (); FS. close (); FS. dispose (); stream. close (); stream. dispose (); webresponse. close (); httpwebrequest. abort (); httpwebrequest = NULL;
3. Merge files (usually lock ))
Filestream FS = new filestream (destinationfilefullname, filemode. append, fileaccess. write); filestream fstemp = NULL; int32 readbytes; byte [] bytes = new byte [this. buffersize]; for (int32 idx = 1; idx <threadcount; idx ++) // merge the temporary files received by the second thread {fstemp = new filestream (threadfilename [idx], filemode. open, fileaccess. read); readbytes = fstemp. read (bytes, 0, this. buffersize); While (readbytes> 0) {FS. write (bytes, 0, readbytes); readbytes = fstemp. read (bytes, 0, this. buffersize);} fstemp. close (); file. delete (threadfilename [idx]);} FS. close (); FS. dispose ();