C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Data;
  6. using System.IO;
  7. using System.ComponentModel;
  8. namespace Common
  9. {
  10.     public class FTPClient
  11.     {
  12.         private string ftpServerIP = String.Empty;
  13.         private string ftpUser = String.Empty;
  14.         private string ftpPassword = String.Empty;
  15.         private string ftpRootURL = String.Empty;        
  16.         public FTPClient(string url, string userid,string password)
  17.         {
  18.             this.ftpServerIP = ftp的IP地址;
  19.             this.ftpUser = 用户名;
  20.             this.ftpPassword = 密码;
  21.             this.ftpRootURL = "ftp://" + url + "/";
  22.         }
  23.         /// <summary>
  24.         /// 上传
  25.         /// </summary>
  26.         /// <param name="localFile">本地文件绝对路径</param>
  27.         /// <param name="ftpPath">上传到ftp的路径</param>
  28.         /// <param name="ftpFileName">上传到ftp的文件名</param>
  29.         public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName)
  30.         {
  31.             bool success = false;
  32.             FtpWebRequest ftpWebRequest = null;
  33.             FileStream localFileStream = null;
  34.             Stream requestStream = null;
  35.             try
  36.             {
  37.                 string uri = ftpRootURL + ftpPath + ftpFileName;
  38.                 ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  39.                 ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  40.                 ftpWebRequest.UseBinary = true;
  41.                 ftpWebRequest.KeepAlive = false;
  42.                 ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
  43.                 ftpWebRequest.ContentLength = localFile.Length;
  44.                 int buffLength = 2048;
  45.                 byte[] buff = new byte[buffLength];
  46.                 int contentLen;
  47.                 localFileStream = localFile.OpenRead();
  48.                 requestStream = ftpWebRequest.GetRequestStream();
  49.                 contentLen = localFileStream.Read(buff, 0, buffLength);
  50.                 while (contentLen != 0)
  51.                 {
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#FtpWebRequest类来实现FTP匿名登录并下载文件。下面是一个示例代码: ```csharp using System; using System.IO; using System.Net; class Program { static void Main(string[] args) { string ftpUrl = "ftp://example.com/file.txt"; string localPath = "C:\\local\\file.txt"; try { // 创建FTP请求对象 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential("anonymous", ""); // 获取FTP服务器的响应 using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { // 打开本地文件流 using (Stream stream = response.GetResponseStream()) { // 创建本地文件 using (FileStream fileStream = new FileStream(localPath, FileMode.Create)) { byte[] buffer = new byte[1024]; int bytesRead = 0; // 从FTP服务器读取数据,并写入本地文件 while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { fileStream.Write(buffer, 0, bytesRead); } } } } Console.WriteLine("文件下载成功!"); } catch (Exception ex) { Console.WriteLine("文件下载失败: " + ex.Message); } } } ``` 请将示例代码中的 `ftp://example.com/file.txt` 替换为实际的FTP服务器地址和文件路径,将 `C:\\local\\file.txt` 替换为本地保存文件的路径和文件名。这段代码会使用匿名登录凭据从FTP服务器下载文件,并将其保存到本地路径中。 注意:匿名登录是通过提供 "anonymous" 作为用户名和空字符串作为密码来实现的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值