博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FTPHelper-封装FTP的相关操作
阅读量:6302 次
发布时间:2019-06-22

本文共 15400 字,大约阅读时间需要 51 分钟。

hot3.png

 
using System;using System.IO;using System.Net;using System.Text;namespace Whir.Software.DataSyncTools.Library.Helper{    ///     ///     Ftp辅助类    ///     public class FtpHelper    {        private const int BufferSize = 2048;        private readonly string _host;        private readonly string _pass;        private readonly string _user;        private FtpWebRequest _ftpRequest;        private FtpWebResponse _ftpResponse;        private Stream _ftpStream;        public FtpHelper(string hostIp, string userName, string password)        {            _host = hostIp;            _user = userName;            _pass = password;        }        ///         ///     下载文件        ///         ///         ///         /// 
public FtpResult Download(string localFile, string remoteFile) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpStream = _ftpResponse.GetResponseStream(); var localFileStream = new FileStream(localFile, FileMode.Create); var byteBuffer = new byte[BufferSize]; if (_ftpStream != null) { int bytesRead = _ftpStream.Read(byteBuffer, 0, BufferSize); try { while (bytesRead > 0) { localFileStream.Write(byteBuffer, 0, bytesRead); bytesRead = _ftpStream.Read(byteBuffer, 0, BufferSize); } } catch (Exception ex) { result = new FtpResult(false, ex.Message); return result; } } localFileStream.Close(); if (_ftpStream != null) _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; result = new FtpResult(true, "ok"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 上传文件 /// /// /// ///
public FtpResult Upload(string localFile, string remoteFile) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; _ftpStream = _ftpRequest.GetRequestStream(); var localFileStream = new FileStream(localFile, FileMode.Create); var byteBuffer = new byte[BufferSize]; int bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize); try { while (bytesSent != 0) { _ftpStream.Write(byteBuffer, 0, bytesSent); bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize); } } catch (Exception ex) { result = new FtpResult(false, ex.Message); return result; } localFileStream.Close(); _ftpStream.Close(); _ftpRequest = null; result = new FtpResult(true, "ok"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 删除文件 /// /// public FtpResult Delete(string deleteFile) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + deleteFile); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpResponse.Close(); _ftpRequest = null; result = new FtpResult(true, "ok"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 文件重命名 /// /// /// ///
public FtpResult Rename(string currentFileNameAndPath, string newFileName) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + currentFileNameAndPath); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.Rename; _ftpRequest.RenameTo = newFileName; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpResponse.Close(); _ftpRequest = null; result = new FtpResult(true, "ok"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 创建目录 /// /// ///
public FtpResult CreateDirectory(string newDirectory) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + newDirectory); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpResponse.Close(); _ftpRequest = null; result = new FtpResult(true, "ok"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 取得文件创建时间 /// /// ///
public FtpResult GetFileCreatedDateTime(string fileName) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + fileName); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpStream = _ftpResponse.GetResponseStream(); if (_ftpStream != null) { var ftpReader = new StreamReader(_ftpStream); string fileInfo; try { fileInfo = ftpReader.ReadToEnd(); } catch (Exception ex) { result = new FtpResult(false, ex.Message); ftpReader.Close(); if (_ftpStream != null) _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; return result; } ftpReader.Close(); if (_ftpStream != null) _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; return new FtpResult(true, fileInfo); } return new FtpResult(false, "响应流为空"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 取得文件大小 /// /// ///
public FtpResult GetFileSize(string fileName) { FtpResult result; try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + fileName); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpStream = _ftpResponse.GetResponseStream(); if (_ftpStream != null) { var ftpReader = new StreamReader(_ftpStream); string fileInfo = null; try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } } catch (Exception ex) { result = new FtpResult(false, ex.Message); ftpReader.Close(); if (_ftpStream != null) _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; return result; } ftpReader.Close(); _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; return new FtpResult(true, fileInfo); } result = new FtpResult(false, "响应流为空"); } catch (Exception ex) { result = new FtpResult(false, ex.Message); } return result; } /// /// 显示远程目录结构 /// /// ///
public string[] DirectoryListSimple(string directory) { try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directory); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpStream = _ftpResponse.GetResponseStream(); if (_ftpStream != null) { var ftpReader = new StreamReader(_ftpStream); string directoryRaw = null; try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } ftpReader.Close(); _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */ try { if (directoryRaw != null) { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return new[] { "" }; } /// /// 远程文件列表 /// /// ///
public string[] DirectoryListDetailed(string directory) { try { _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directory); _ftpRequest.Credentials = new NetworkCredential(_user, _pass); _ftpRequest.UseBinary = true; _ftpRequest.UsePassive = true; _ftpRequest.KeepAlive = true; _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse(); _ftpStream = _ftpResponse.GetResponseStream(); if (_ftpStream != null) { var ftpReader = new StreamReader(_ftpStream); string directoryRaw = null; try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } ftpReader.Close(); _ftpStream.Close(); _ftpResponse.Close(); _ftpRequest = null; try { if (directoryRaw != null) { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } /* Return an Empty string Array if an Exception Occurs */ return new[] { "" }; } } public class FtpResult { public FtpResult(bool isCusecess, string message) { IsSucess = isCusecess; Message = message; } public bool IsSucess { get; set; } public string Message { get; set; } }}

转载于:https://my.oschina.net/zhangqs008/blog/712743

你可能感兴趣的文章
Git笔记
查看>>
普通人如何从平庸到优秀,在到卓越
查看>>
SLAM数据集
查看>>
c#学习笔记05——数组&集合
查看>>
【图论算法】Dijstra&BFS
查看>>
注册和上传文件(头像)
查看>>
使用OVS
查看>>
键盘回收的几种方法
查看>>
Linux help websites
查看>>
ansible - roles(高级运维)
查看>>
Python(条件判断和循环)
查看>>
使用Html5开发Windows 8应用
查看>>
day4 linux安装python
查看>>
Spark 架构原理介绍 以及 job、task、stag 概念
查看>>
LeetCode Container With Most Water (Two Pointers)
查看>>
npm install (让别人下载自己的包)
查看>>
vue (v-if show 问题)
查看>>
转://Oracle 高可用技术与云基础架构
查看>>
[转载] 七龙珠第一部——第080话 悟空对抗天龙
查看>>
Spring 并发事务的探究
查看>>