[C#] C#利用WebClient 两种方式下载文件 →→→→→进入此内容的聊天室

来自 , 2020-05-27, 写在 C#, 查看 113 次.
URL http://www.code666.cn/view/a431d701
  1.  WebClient client = new WebClient();
  2.  
  3. 第一种
  4.  
  5. string URLAddress = @"http://files.cnblogs.com/x4646/tree.zip";
  6.  
  7. string receivePath=@"C:\";
  8.  
  9. client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
  10.  
  11. 就OK了。
  12.  
  13. 第二种
  14.  
  15.  Stream str = client.OpenRead(URLAddress);
  16.    StreamReader reader = new StreamReader(str);
  17.    byte[] mbyte = new byte[1000000];
  18.    int allmybyte = (int)mbyte.Length;
  19.    int startmbyte = 0;
  20.  
  21.    while (allmybyte > 0)
  22.    {
  23.  
  24.     int m = str.Read(mbyte, startmbyte, allmybyte);
  25.     if (m == 0)
  26.      break;
  27.  
  28.     startmbyte += m;
  29.     allmybyte -= m;
  30.    }
  31.  
  32.    reader.Dispose();
  33.    str.Dispose();
  34.  
  35.    string path = receivePath + System.IO.Path.GetFileName(URLAddress);
  36.    FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  37.    fstr.Write(mbyte, 0, startmbyte);
  38.    fstr.Flush();
  39.    fstr.Close();
  40.  
  41.  

回复 "C#利用WebClient 两种方式下载文件"

这儿你可以回复上面这条便签

captcha