lang110 发表于 2015-5-29 11:15:24

FTP 下载功能代码

  FTP的下载功能代码,一个小方法代码
  

Code
      publicvoid TestFTPDownload()
      {
            try
            {
                // go get the same code from edition 1
                FtpWebRequest request =
                  (FtpWebRequest)WebRequest.Create(
                  "ftp://ftp.oreilly.com/pub/examples/csharpckbk/CSharpCookbook.zip");

                request.Credentials = new NetworkCredential("anonymous", "hilyard@oreilly.com");
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                  Stream data = response.GetResponseStream();
                  string targetPath = "CSharpCookbook.zip";
                  if (File.Exists(targetPath))
                        File.Delete(targetPath);

                  byte[] byteBuffer = new byte;
                  using (FileStream output = new FileStream(targetPath, FileMode.CreateNew))
                  {
                        int bytesRead = 0;
                        do
                        {
                            bytesRead = data.Read(byteBuffer, 0, byteBuffer.Length);
                            if (bytesRead > 0)
                            {
                              output.Write(byteBuffer, 0, bytesRead);
                            }
                        }
                        while (bytesRead > 0);
                  }
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(e);
            }
      }
页: [1]
查看完整版本: FTP 下载功能代码