using System;
using System.IO;
using System.Net;
using System.Text;
namespace Whir.Software.DataSyncTools.Library.Helper
{
/// <summary>
/// Ftp辅助类
/// </summary>
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;
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="localFile"></param>
/// <param name="remoteFile"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="localFile"></param>
/// <param name="remoteFile"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="deleteFile"></param>
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;
}
/// <summary>
/// 文件重命名
/// </summary>
/// <param name="currentFileNameAndPath"></param>
/// <param name="newFileName"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="newDirectory"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 取得文件创建时间
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 取得文件大小
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 显示远程目录结构
/// </summary>
/// <param name="directory"></param>
/// <returns></returns>
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[] { "" };
}
/// <summary>
/// 远程文件列表
/// </summary>
/// <param name="directory"></param>
/// <returns></returns>
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; }
}
}
分享到:
相关推荐
C# FTP操作帮助类, FTPHelper.cs,已经封装好了FTP相关的各个操作的方法
FTP 上传/下载 upload/download util 工具类 FTPhelper ,写好的类
主要为大家详细介绍了FtpHelper实现ftp服务器文件读写操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
FtpHelper /// /// Gets a list of the files on an FTP server. /// /// <param name="remotePath">The path on an FTP server. /// <param name="recursive">true to get all files from ...
FTPHelper-FTP帮助类,FTP常用操作方法,添加文件,删除文件等 FTPOperater FTP操作帮助类,方法比较多,比较实用 6.JS操作类 JsHelper JsHelper--Javascript操作帮助类,输出各种JS方法,方便不懂JS的人使用,...
C# FtpHelper 上传 ftp上传简单封装,目前有三个项目在用
FTP下载上传操作类。通过该类可以浏览FTP文件,并上传/下载需要的文件
适合刚接触C#学习,直接利用的人
FTPHelper-FTP帮助类,FTP常用操作方法,添加文件,删除文件等 FTPOperater FTP操作帮助类,方法比较多,比较实用 6.JS操作类 JsHelper JsHelper--Javascript操作帮助类,输出各种JS方法,方便不懂JS的人使用,...
FTPHelper帮助类,网上盛传的那个类不好说,反正我用过觉得不方便,因此自己花了点时间自己写了一个,方便大家使用,对新手也是个很好的学习编码的教程
java实现文件上传到fpt,里面一些很简单的操作适合初学者。
常用的FTP类,可自行改进.支持异步上传下载....但不支持断点.
FTP操作辅助类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮助(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅助类(ProxyHelper.cs) ----------Winform-------------- 跨线程的...
1.最近项目开发涉及到ftp操作。发现FtpWebRequest类有些操作很麻烦, 例如创建目录只能创建一级目录,多级就失败。删除目录只能删除最后一级的空目录。同样下载也是。 故写下帮助类方便使用。 2.调用方法展示, ...
FTP操作辅助类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮助(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅助类(ProxyHelper.cs) ----------Winform-------------- 跨线程的...
FTP操作辅助类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮助(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅助类(ProxyHelper.cs) ----------Winform-------------- 跨线程的...
FTP操作辅助类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮助(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅助类(ProxyHelper.cs) ----------Winform-------------- 跨线程的控件安全...
FTPHelper-FTP帮助类,FTP常用操作方法,添加文件,删除文件等 FTPOperater FTP操作帮助类,方法比较多,比较实用 6.JS操作类 JsHelper JsHelper--Javascript操作帮助类,输出各种JS方法,方便不懂JS的人使用,...
FTP代码。已在实际项目中使用。支持二进制与文本两种格式。