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

2 篇文章 0 订阅
1 篇文章 0 订阅

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

分类: C#编程 4245人阅读 评论(0) 收藏 举报
  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.                 {
  52.                     requestStream.Write(buff, 0, contentLen);
  53.                     contentLen = localFileStream.Read(buff, 0, buffLength);
  54.                 }
  55.                 success = true;
  56.             }
  57.             catch (Exception)
  58.             {
  59.                 success = false;
  60.             }
  61.             finally
  62.             { 
  63.                 if (requestStream != null)
  64.                 {
  65.                     requestStream.Close();
  66.                 }
  67.                 if (localFileStream != null)
  68.                 {
  69.                     localFileStream.Close();
  70.                 }
  71.             }
  72.             return success;
  73.         }
  74.         /// <summary>
  75.         /// 上传文件
  76.         /// </summary>
  77.         /// <param name="localPath">本地文件地址(没有文件名)</param>
  78.         /// <param name="localFileName">本地文件名</param>
  79.         /// <param name="ftpPath">上传到ftp的路径</param>
  80.         /// <param name="ftpFileName">上传到ftp的文件名</param>
  81.         public bool fileUpload(string localPath, string localFileName, string ftpPath, string ftpFileName)
  82.         {
  83.             bool success = false;
  84.             try
  85.             {
  86.                 FileInfo localFile = new FileInfo(localPath + localFileName);
  87.                 if (localFile.Exists)
  88.                 {
  89.                     success = fileUpload(localFile, ftpPath, ftpFileName);
  90.                 }
  91.                 else
  92.                 {
  93.                     success = false;
  94.                 }
  95.             }
  96.             catch (Exception)
  97.             {
  98.                 success = false;
  99.             }
  100.             return success;
  101.         }
  102.         /// <summary>
  103.         /// 下载文件
  104.         /// </summary>
  105.         /// <param name="localPath">本地文件地址(没有文件名)</param>
  106.         /// <param name="localFileName">本地文件名</param>
  107.         /// <param name="ftpPath">下载的ftp的路径</param>
  108.         /// <param name="ftpFileName">下载的ftp的文件名</param>
  109.         public bool fileDownload(string localPath, string localFileName, string ftpPath, string ftpFileName)
  110.         {
  111.             bool success = false;
  112.             FtpWebRequest ftpWebRequest = null;
  113.             FtpWebResponse ftpWebResponse = null;
  114.             Stream ftpResponseStream = null;
  115.             FileStream outputStream = null;
  116.             try
  117.             {
  118.                 outputStream = new FileStream(localPath + localFileName, FileMode.Create);
  119.                 string uri = ftpRootURL + ftpPath + ftpFileName;
  120.                 ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  121.                 ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  122.                 ftpWebRequest.UseBinary = true;
  123.                 ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  124.                 ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  125.                 ftpResponseStream = ftpWebResponse.GetResponseStream();
  126.                 long contentLength = ftpWebResponse.ContentLength;
  127.                 int bufferSize = 2048;
  128.                 byte[] buffer = new byte[bufferSize];
  129.                 int readCount;
  130.                 readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
  131.                 while (readCount > 0)
  132.                 {
  133.                     outputStream.Write(buffer, 0, readCount);
  134.                     readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
  135.                 }
  136.                 success = true;
  137.             }
  138.             catch (Exception)
  139.             {
  140.                 success = false;
  141.             }
  142.             finally
  143.             {
  144.                 if (outputStream != null)
  145.                 {
  146.                     outputStream.Close();
  147.                 }
  148.                 if (ftpResponseStream != null)
  149.                 {
  150.                     ftpResponseStream.Close();
  151.                 }
  152.                 if (ftpWebResponse != null)
  153.                 {
  154.                     ftpWebResponse.Close();
  155.                 }
  156.             }
  157.             return success;
  158.         }
  159.         /// <summary>
  160.         /// 重命名
  161.         /// </summary>
  162.         /// <param name="ftpPath">ftp文件路径</param>
  163.         /// <param name="currentFilename"></param>
  164.         /// <param name="newFilename"></param>
  165.         public bool fileRename(string ftpPath, string currentFileName, string newFileName)
  166.         {
  167.             bool success = false;
  168.             FtpWebRequest ftpWebRequest = null;
  169.             FtpWebResponse ftpWebResponse = null;
  170.             Stream ftpResponseStream = null;
  171.             try
  172.             {
  173.                 string uri = ftpRootURL + ftpPath + currentFileName;
  174.                 ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  175.                 ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  176.                 ftpWebRequest.UseBinary = true;
  177.                 ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;
  178.                 ftpWebRequest.RenameTo = newFileName;
  179.                 
  180.                 ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  181.                 ftpResponseStream = ftpWebResponse.GetResponseStream();
  182.                
  183.             }
  184.             catch (Exception)
  185.             {
  186.                 success = false;
  187.             }
  188.             finally
  189.             {
  190.                 if (ftpResponseStream != null)
  191.                 {
  192.                     ftpResponseStream.Close();
  193.                 }
  194.                 if (ftpWebResponse != null)
  195.                 {
  196.                     ftpWebResponse.Close();
  197.                 }
  198.             }
  199.             return success;
  200.         }
  201.         /// <summary>
  202.         /// 消除文件
  203.         /// </summary>
  204.         /// <param name="filePath"></param>
  205.         public bool fileDelete(string ftpPath, string ftpName)
  206.         {
  207.             bool success = false;
  208.             FtpWebRequest ftpWebRequest = null;
  209.             FtpWebResponse ftpWebResponse = null;
  210.             Stream ftpResponseStream = null;
  211.             StreamReader streamReader = null;
  212.             try
  213.             {
  214.                 string uri = ftpRootURL + ftpPath + ftpName;
  215.                 ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  216.                 ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  217.                 ftpWebRequest.KeepAlive = false;
  218.                 ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  219.                 ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  220.                 long size = ftpWebResponse.ContentLength;
  221.                 ftpResponseStream = ftpWebResponse.GetResponseStream();
  222.                 streamReader = new StreamReader(ftpResponseStream);
  223.                 string result = String.Empty;
  224.                 result = streamReader.ReadToEnd();
  225.                
  226.                 success = true;
  227.             }
  228.             catch (Exception)
  229.             {
  230.                 success = false;
  231.             }
  232.             finally
  233.             {
  234.                 if (streamReader != null)
  235.                 {
  236.                     streamReader.Close();
  237.                 }
  238.                 if (ftpResponseStream != null)
  239.                 {
  240.                     ftpResponseStream.Close();
  241.                 }
  242.                 if (ftpWebResponse != null)
  243.                 {
  244.                     ftpWebResponse.Close();
  245.                 }
  246.             }
  247.             return success;
  248.         }
  249.         /// <summary>
  250.         /// 文件存在检查
  251.         /// </summary>
  252.         /// <param name="ftpPath"></param>
  253.         /// <param name="ftpName"></param>
  254.         /// <returns></returns>
  255.         public bool fileCheckExist(string ftpPath, string ftpName)
  256.         {
  257.             bool success = false;
  258.             FtpWebRequest ftpWebRequest = null;
  259.             WebResponse webResponse = null;            
  260.             StreamReader reader = null;
  261.             try
  262.             {
  263.                 string url = ftpRootURL + ftpPath;                
  264.                 
  265.                 ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  266.                 ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  267.                 ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  268.                 ftpWebRequest.KeepAlive = false;
  269.                 webResponse = ftpWebRequest.GetResponse();
  270.                 reader = new StreamReader(webResponse.GetResponseStream());
  271.                 string line = reader.ReadLine();
  272.                 while (line != null)
  273.                 {
  274.                     if (line == ftpName)
  275.                     {
  276.                         success = true;
  277.                         break;
  278.                     }
  279.                     line = reader.ReadLine();
  280.                 }
  281.             }
  282.             catch (Exception)
  283.             {
  284.                 success = false;
  285.             }
  286.             finally
  287.             {
  288.                 if (reader != null)
  289.                 {
  290.                     reader.Close();
  291.                 }
  292.                 if (webResponse != null)
  293.                 {
  294.                     webResponse.Close();
  295.                 }
  296.             }
  297.             return success;
  298.         }
  299.     }
  300. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值