网络文件操作

0、获取网络IP、端口:

            NameValueCollection nameValueTable = new NameValueCollection();

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
                nameValueTable = HttpUtility.ParseQueryString(queryString);
            }

 nameValueTable["IP"];

1、获取网络文件大小:

                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、下载文件

         Byte[] bytes = new Byte[this.bufferSize];
              Int32 readBytes = 0;
              FileStream fs = new FileStream(threadFileName[currentThreadIndex], FileMode.Create, FileAccess.Write);
          
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;)"; //接收的起始位置及接收的长度 httpWebRequest.AddRange(Convert.ToInt32(threadFileStart[currentThreadIndex]), Convert.ToInt32(threadFileStart[currentThreadIndex] + threadFileSize[currentThreadIndex] - 1)); WebResponse webResponse = httpWebRequest.GetResponse(); Stream stream = webResponse.GetResponseStream();//获得接收流 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、合并文件(一般要加锁(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++) //从第二个线程接收的临时文件开始合并
                    {
                        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();
原文地址:https://www.cnblogs.com/shenchao/p/3912317.html