自己在用的FTP类,实现了检查FTP链接以及返回FTP没有反应的情况。

 public delegate void ShowError(string content, string title);
/// <summary>
/// 使用FtpWebRequest进行FTP操作
/// </summary>
public class VeviFtpHelper:IDisposable
{
string ftpHost;//FTP HostName or IPAddress
string ftpUserID;
string ftpPassword;
int port = 21;
string ftpName;
/// <summary>
/// FTP根目录URI
/// </summary>
string ftpRootURI;
/// <summary>
/// FTP的IP地址
/// </summary>
string ftpIPAddress;
FtpWebRequest reqFTP;
//
bool isconnect = false;
#region 属性
/// <summary>
/// FTP根目录URI
/// </summary>
public string FTPRootURI
{
get { return ftpRootURI; }
}
#endregion
#region 事件
public event ShowError ShowErrorEvent;
private void ErrorNotify(string content, string title)
{
if (ShowErrorEvent != null)
ShowErrorEvent(content, title);
}
#endregion
#region 构造函数
public VeviFtpHelper(string hostName, string userID, string passWord)
{
ftpHost = hostName;
ftpUserID = userID;
ftpPassword = passWord;
PrepareFTPInfo();
}
public VeviFtpHelper(string hostName, string userID, string passWord, int port):this(hostName,userID,passWord)
{
this.port = port;
PrepareFTPInfo();
}
public VeviFtpHelper(string hostName, string ftpName, string userID, string passWord)
: this(hostName, userID, passWord)
{
this.ftpName = ftpName;
PrepareFTPInfo();
}
public VeviFtpHelper(string hostName, string ftpName, string userID, string passWord, int port)
: this(hostName,ftpName, userID, passWord)
{
this.port = port;
this.ftpName = ftpName;
PrepareFTPInfo();
}
#endregion
public bool PrepareFTPInfo()
{
try
{
IPEndPoint remoteEndPoint = new IPEndPoint
(IPAddress.Parse(ftpHost), port);
ftpIPAddress = remoteEndPoint.Address.ToString();
ftpRootURI = "ftp://" + ftpIPAddress + "/";
if (!string.IsNullOrEmpty(ftpName))
ftpRootURI += ftpName + "/";
}
catch (FormatException)
{
ErrorNotify("FTP地址的格式不正确,或者连接不到FTP主机。", "FTP地址错误");
return false;
}
catch (Exception ex)
{
ErrorNotify(ex.Message, "连接错误");
return false;
}
return true;
}
private bool Connect(string path)
{
try
{
if (!PrepareFTPInfo())
return false;
if (string.IsNullOrEmpty(path))
path = ftpRootURI;
//connect to ftp
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// 指定数据传输类型
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
//
isconnect = true;
}
catch (FormatException)
{
DisConnect(); ErrorNotify("FTP地址的格式不正确,或者连接不到FTP主机。", "FTP地址错误");
return false;
}
catch (Exception ex)
{
DisConnect(); ErrorNotify(ex.Message, "连接错误");
return false;
}
return true;
}
private void DisConnect()
{
isconnect = false;
}
#region Methods
#region 判断是否存在
/// <summary>
/// FTP测试
/// </summary>
/// <returns></returns>
public bool ExistFTP()
{
if (!Connect(""))
{
return false;
}
try
{
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader
(response.GetResponseStream(), System.Text.Encoding.Default);
string line = reader.ReadLine();
return true;
}
catch (Exception e1)
{
DisConnect();
ErrorNotify(e1.Message, "连接失败");
return false;
}
}
/// <summary>
/// FTP是否存在<paramref name="path"/>的路径
/// </summary>
/// <param name="path">ftp路径</param>
/// <returns></returns>
public bool ExistPath(string path)
{
if (!Connect(path))
return false;
try
{
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader
(response.GetResponseStream(), System.Text.Encoding.Default);
string line = reader.ReadLine();
DisConnect();
return true;
}
catch (Exception e1)
{
ErrorNotify("FTP路径不存在!\r\n" + e1.Message, "连接失败");
DisConnect();
return false;
}
}
/// <summary>
/// 是否存在文件
/// </summary>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <param name="info"></param>
/// <returns></returns>
public bool ExistFile(string path, string fileName, out string info)
{
return GetFileList(path, out info).Contains(fileName);
}
public bool ExistFile(string path, string fileName)
{
string info;
return GetFileList(path, out info).Contains(fileName);
}
/// <summary>
/// 是否存在文件夹
/// </summary>
/// <param name="path"></param>
/// <param name="folderName"></param>
/// <param name="info"></param>
/// <returns></returns>
public bool ExistFolder(string path, string folderName, out string info)
{
return GetFolderList(path, out info).Contains(folderName);
}
#endregion
#region 获取信息
/// <summary>
/// 获取文件列表
/// </summary>
/// <param name="path">ftp目录路径</param>
/// <param name="info">0 failed 1 success</param>
/// <returns>文件名称列表</returns>
public IList<string> GetFileList(string path, out string info)
{
return GetFTPPathList(path, "file", out info);
}
/// <summary>
/// 获取目录的列表
/// </summary>
/// <param name="path">ftp目录路径</param>
/// <param name="info">0 failed 1 success</param>
/// <returns>目录的列表</returns>
public IList<string> GetFolderList(string path, out string info)
{
return GetFTPPathList(path, "folder", out info);
} /// <summary>
/// 获取FTP<paramref name="path"/>下的所有内容
/// </summary>
/// <param name="path">Ftp路径</param>
/// <param name="getType">0/file is File 1/Folder is Folder </param>
/// <param name="info">0 failed 1 success</param>
/// <returns>文件或文件夹列表</returns>
public IList<string> GetFTPPathList(string path, string getType, out string info)
{
IList<string> fileList = new List<string>();
//
if (!Connect(path))
{
info = "0";
return fileList;
}
try
{
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader
(response.GetResponseStream(), System.Text.Encoding.Default);
string line = reader.ReadLine();
while (line != null)
{
if (!string.IsNullOrEmpty(line))
{
bool flag;//ture is folder false is file
string value = GetListInfo(line, out flag);
if (flag == (getType == "1" || getType.Trim().ToLower() == "folder"))
fileList.Add(value);
}
line = reader.ReadLine();
}
// to remove the trailing '/n'
DisConnect();
reader.Close();
response.Close();
info = "1";
return fileList;
}
catch (Exception ex)
{
//System.Windows.Forms.MessageBox.Show(ex.Message);
ErrorNotify("路径不存在\r\n" + ex.Message, "获取文件夹");
fileList = new List<string>();
info = "0";
DisConnect();
return fileList;
}
}
/// <summary>
/// 处理得到的信息流,判断是文件还是文件夹
/// </summary>
/// <param name="line"></param>
/// <param name="isfloder"></param>
/// <returns></returns>
private string GetListInfo(string line, out bool isfloder)
{
string processstr = line.Trim();
string dateStr = processstr.Substring(0, 8);
processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
string timeStr = processstr.Substring(0, 7);
processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;
myDTFI.ShortTimePattern = "t";
DateTime CreateTime = DateTime.Parse(dateStr + " " + timeStr, myDTFI); if (processstr.Substring(0, 5) == "<DIR>")
{
isfloder = true;
processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
return processstr;
}
else
{
isfloder = false;
int idx = processstr.IndexOf(" ");
processstr = processstr.Substring(idx, processstr.Length - idx).Trim();
//string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);  // true);     
//processstr = strs[1];
return processstr;
}
}
#endregion
#region 文件操作
/// <summary>
/// 上传文件
/// </summary>
/// <param name="path">文件上传到的ftp路径</param>
/// <param name="oriFilePath">源文件地址</param>
/// <param name="ftpFileName">ftp中此文件的名称</param>
/// <returns>是否成功</returns>
public bool UploadFile(string path, string oriFilePath, string ftpFileName)
{
string info;
if (ExistFile(path, ftpFileName, out info))
{
info = "0";
ErrorNotify("文件已存在,请重新命名文件!", "上传文件");
return false;
}
else
{
if (info != "1")
return false;
}
FileInfo fileInf = new FileInfo(oriFilePath);
//上传文件路径名称
string ftpFilePath = (path.EndsWith("/") ? path : path + "/") + ftpFileName;
//
if (!Connect(ftpFilePath))
{
info = "0";
return false;
}
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
DisConnect();
info = "1";
return true;
}
catch (Exception ex)
{
ErrorNotify(ex.Message, "上传文件");
info = "0";
DisConnect();
return false;
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="path">文件上传到的ftp路径</param>
/// <param name="oriFilePath">源文件地址</param>
/// <returns>是否成功</returns>
public bool UploadFile(string path, string oriFilePath)
{
string ftpFileName = System.IO.Path.GetFileName(oriFilePath);
return UploadFile(path, oriFilePath, ftpFileName);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool DeleteFile(string filePath)
{
if (!Connect(filePath))
{
return false;
}
try
{
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
//
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
DisConnect();
return true;
}
catch (Exception ex)
{
ErrorNotify("删除文件失败!\r\n" + ex.Message, "删除文件");
DisConnect();
return false;
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="ftpFilePath">ftp中文件的路径,包含文件名</param>
/// <param name="savePath">保存下载文件的路径</param>
/// <returns></returns>
public bool DownLoadFile(string ftpFilePath, string savePath)
{
string fileName = System.IO.Path.GetFileName(ftpFilePath);
return DownLoadFile(ftpFilePath, savePath, fileName);
}
/// <summary>
///
/// </summary>
/// <param name="ftpFilePath">ftp中文件的路径,包含文件名</param>
/// <param name="savePath">保存下载文件的路径</param>
/// <param name="downloadFileName">保存文件的名称</param>
/// <returns></returns>
public bool DownLoadFile(string ftpFilePath, string savePath, string downloadFileName)
{
if (!Connect(ftpFilePath))
{
return false;
}
FileStream outputStream=null;// = new FileStream(savePath + "\\" + downloadFileName, FileMode.Create);
try
{
outputStream = new FileStream(savePath + "\\" + downloadFileName, FileMode.Create);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
} ftpStream.Close();
outputStream.Close();
response.Close();
DisConnect();
return true;
}
catch (Exception ex)
{
if (outputStream != null)
{
outputStream.Close();
if (File.Exists(savePath + "\\" + downloadFileName))
File.Delete(savePath + "\\" + downloadFileName);
}
ErrorNotify("下载文件失败!\r\n" + ex.Message, "下载文件");
DisConnect();
return false;
}
}
#endregion
#region 创建目录
/// <summary>
/// 创建目录
/// </summary>
/// <param name="path">当前FTP目录</param>
/// <param name="folderName">文件夹名称</param>
/// <returns></returns>
public bool CreateFolder(string path, string folderName)
{
string info;
if (ExistFolder(path, folderName, out info))
{
info = "1";
ErrorNotify("文件夹已存在!", "创建文件夹");
return true;
}
else
{
if (info != "1")
return false;
}
//
string ftpFolderpath = (path.EndsWith("/") ? path : path + "/") + folderName;
//
if (!Connect(ftpFolderpath))
{
info = "0";
return false;
}
try
{
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
//
ftpStream.Close();
response.Close();
info = "1";
DisConnect();
return true;
}
catch (Exception e1)
{
ErrorNotify(e1.Message, "创建文件夹");
info = "0";
DisConnect();
return false;
}
}
/// <summary>
/// 创建目录
/// </summary>
/// <param name="folderPath">FTP目录</param>
/// <returns></returns>
public bool CreateFolder(string folderPath)
{
int idx = folderPath.LastIndexOf("/");
string path = folderPath.Substring(0, idx);
string folderName = folderPath.Substring(idx, folderPath.Length - idx);
return CreateFolder(path, folderName);
} public bool DeleteFolder(string ftpPath)
{
if (ftpPath == ftpRootURI)
{
ErrorNotify("FTP根目录无法删除!", "删除文件夹");
return false;
}
//遍历所有的子文件夹
string info;
IList<string> folderList = GetFolderList(ftpPath, out info);
if (info == "1")
{
foreach (string folderName in folderList)
{
string newPath = (ftpPath.EndsWith("/") ? ftpPath : ftpPath + "/") + folderName;
if (!DeleteFolder(newPath))
return false;
}
}
else//连接出错
return false;
//删除当前目录下的文件
IList<string> fileList = GetFileList(ftpPath, out info);
if (info == "1")
{
foreach (string fileName in fileList)
if (!DeleteFile((ftpPath.EndsWith("/") ? ftpPath : ftpPath + "/") + fileName))
return false;
}
else return false;
//删除自己
return DeleteOnlyFolder(ftpPath); }
/// <summary>
/// 只删除文件夹
/// </summary>
/// <param name="ftpPath"></param>
/// <returns></returns>
private bool DeleteOnlyFolder(string ftpPath)
{
if (!Connect(ftpPath))
{
return false;
}
try
{
reqFTP.UseBinary = true;
//reqFTP.UsePassive = false;
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
DisConnect();
return true;
}
catch (Exception ex)
{
ErrorNotify("删除文件夹失败!\r\n" + ex.Message, "删除文件夹");
DisConnect();
return false;
}
}
#endregion
/// <summary>
/// 重命名文件名或文件夹名称
/// </summary>
/// <param name="currentFtpFilePath"></param>
/// <param name="newFilename">新的文件名或文件夹名称</param>
public bool ReName(string currentFtpFilePath, string newFilename)
{
if (!Connect(currentFtpFilePath))
{
return false;
}
try
{
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = newFilename;
reqFTP.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream(); ftpStream.Close();
response.Close();
DisConnect();
return true;
}
catch (Exception ex)
{
ErrorNotify("重命名失败!\r\n" + ex.Message, "重命名");
DisConnect();
return false;
}
}
#endregion #region IDisposable 成员 public void Dispose()
{
if (reqFTP != null)
reqFTP.Abort();
} #endregion
}

关于FTP操作的功能类的更多相关文章

  1. PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  2. C# FTP操作类的代码

    如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...

  3. 【转载】C#工具类:FTP操作辅助类FTPHelper

    FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...

  4. php的FTP操作类

    class_ftp.php <?php /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ftp { public $off; // 返回操作状 ...

  5. [PHP学习教程 - 类库]002.FTP操作(FTP)

    引言:FTP是大家上传至站点服务器必须要使用的协议.现在常用的FTP客户端工具也很多,如:8uftp,FlashFXP,....但是使用客户端工具就无法真正与自动化联系起来.所以今天,我们为大家讲一下 ...

  6. Ubuntu下VSFTPD(六)(常见FTP命令及其功能) (

    常见FTP命令及其功能  FTP 命令 功能  FTP 命令 功能  ls 显示服务器上的目录 ls [remote-dir][local-file] 显示远程目录remote-dir,并存入本地文件 ...

  7. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  8. C# FTP操作

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...

  9. PHP - FTP上传文件类

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

随机推荐

  1. ADO.NET知识的运用一(Day 26)

    哈哈,又到了总结的时间了,来回顾一下今天主要学了关于ADO.NET的哪些知识吧.(这次学的ADO访问数据库主要以访问SQL数据库为主) 理论:  首先我们要知道为什么要学习ADO.NET? 因为我们之 ...

  2. C指针(转)

    第一章 指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址. 要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的 类型,指针的值或者叫指针所指向的内存区,还有 ...

  3. Java web 开发环境配置。

    一.配置 win8 64位 环境java 开发环境 1.  下载JDK,地址 http://www.oracle.com/technetwork/java/javase/downloads/index ...

  4. Oracle SQL篇(一)null值之初体验

           从我第一次正式的写sql语句到现在,已经超过10年的时间了.我写报表,做统计分析和财务对账,我一点点的接触oracle数据库,并尝试深入了解.这条路,一走就是10年,从充满热情,到开始厌 ...

  5. 关于document.selection和TextRange对象的介绍

    document.selection只有IE支持 window.getSelection()也只有FireFox和Safari支持,都不是标准语法. selection 对象代表了当前激活选中区,即高 ...

  6. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  7. 对象图(Object Diagram)—UML图(三)

    一.用一张图来介绍一下对象图的基本内容 二.对象图与类图的基本差别 三.对象图实例

  8. Nutch 二次开发之parse正文内容

    关于nutch的基础知识能够參考lemo的专栏 nutch支持二次开发,为了满足搜索的准确率的问题,考虑只将网页正文的内容提取出来作为索引的内容,相应的是parse_text的数据.我使用的事nutc ...

  9. iOS开发之第三方分享QQ分享,史上最新最全第三方分享QQ方式实现

    本文章源码地址: https://github.com/zhonggaorong/QQLoginDemo 项目搭建参考:  (包含QQ登录源码下载 . QQ sdk集成) http://blog.cs ...

  10. Xcode 新版本如何设置ARC

    在刚刚开始学习IOS开发时,最好不要开启ARC,这样有助于学习内存管理,但不少刚刚接触Xcode的朋友可能会发现,当你使用最新版本的Xcode时,敲入release等代码时会提示报错.这是因为系统默认 ...