FTP下载

#region "Download: File transfer FROM ftp server"         /// <summary>         /// Copy a file from FTP server to local         /// </summary>         /// <param name="sourceFilename"> Target filename, if required </param>         /// <param name="localFilename"> Full path of the local file </param>         /// <returns> </returns>         /// <remarks> Target can be blank (use same filename), or just a filename         /// (assumes current directory) or a full path and filename </remarks>         public bool Download( string sourceFilename, string localFilename, bool PermitOverwrite)         {             // 2. determine target file             FileInfo fi = new FileInfo(localFilename);             return this .Download(sourceFilename, fi, PermitOverwrite);         }         // Version taking an FtpFileInfo         public bool Download(FtpFileInfo file, string localFilename, bool permitOverwrite)         {             return this .Download(file.FullName, localFilename, permitOverwrite);         }         // Another version taking FtpFileInfo and FileInfo         public bool Download(FtpFileInfo file, FileInfo localFI, bool permitOverwrite)         {             return this .Download(file.FullName, localFI, permitOverwrite);         }         // Version taking string/FileInfo         public bool Download( string sourceFilename, FileInfo targetFI, bool permitOverwrite)         {             // 1. check target             if (targetFI.Exists && ! (permitOverwrite))             {                 throw ( new ApplicationException( " Target file already exists " ));             }             // 2. check source             string target;             if (sourceFilename.Trim() == "" )             {                 throw ( new ApplicationException( " File not specified " ));             }             else if (sourceFilename.Contains( " / " ))             {                 // treat as a full path                 target = AdjustDir(sourceFilename);             }             else             {                 // treat as filename only, use current directory                 target = CurrentDirectory + sourceFilename;             }             string URI = Hostname + target;             // 3. perform copy             System.Net.FtpWebRequest ftp = GetRequest(URI);             // Set request to download a file in binary mode             ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;             ftp.UseBinary = true ;             // open request and get response stream             using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())             {                 using (Stream responseStream = response.GetResponseStream())                 {                     // loop to read & write to file                     using (FileStream fs = targetFI.OpenWrite())                     {                         try                         {                             byte [] buffer = new byte [ 2048 ];                             int read = 0 ;                             do                             {                                 read = responseStream.Read(buffer, 0 , buffer.Length);                                 fs.Write(buffer, 0 , read);                             } while ( ! (read == 0 ));                             responseStream.Close();                             fs.Flush();                             fs.Close();                         }                         catch (Exception)                         {                             // catch error and delete file only partially downloaded                             fs.Close();                             // delete target file as it's incomplete                             targetFI.Delete();                             throw ;                         }                     }                     responseStream.Close();                 }                 response.Close();             }             return true ;         }         #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WinForm中实现FTP下载功能的方法如下: 1. 首先,需要引入System.Net命名空间,其中包含了用于FTP操作的相关类和方法。 2. 创建一个WinForm应用程序,添加一个按钮和一个文本框,用于输入FTP服务器的地址和文件路径。 3. 在按钮的Click事件中编写代码,实现FTP下载功能。 ```csharp private void btnDownload_Click(object sender, EventArgs e) { // 获取输入的FTP服务器地址和文件路径 string ftpServer = txtServer.Text; string ftpFilePath = txtFilePath.Text; // 创建用于下载的WebClient对象 WebClient webClient = new WebClient(); try { // 指定需要下载FTP文件的URL string ftpUrl = "ftp://" + ftpServer + "/" + ftpFilePath; // 指定保存下载文件的本地路径 string localFilePath = "C:\\" + Path.GetFileName(ftpFilePath); // 指定FTP服务器的用户名和密码(如果需要认证) webClient.Credentials = new NetworkCredential("用户名", "密码"); // 开始下载文件 webClient.DownloadFile(ftpUrl, localFilePath); MessageBox.Show("下载成功!"); } catch (Exception ex) { MessageBox.Show("下载失败:" + ex.Message); } } ``` 以上代码中,首先获取输入的FTP服务器地址和文件路径,然后创建一个WebClient对象。通过拼接FTP服务器地址和文件路径生成需要下载的文件的URL,指定本地保存路径。如果FTP服务器需要认证,可以设置网络凭据。最后调用`DownloadFile`方法开始下载文件,下载成功后弹出提示框。 需要注意的是,FTP下载操作可能会耗费较长时间,为了避免界面无响应,可以将下载操作放在后台线程进行,并在下载完成后更新界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值