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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值