FTP协议操作代码
采用MS的FTP协议封装
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.DirectoryServices.Protocols;
namespace Vancl.FileUtility
{
public class FtpCmd
{
创建文件夹#region 创建文件夹
/**//// <summary>
/// 创建文件夹:不实现级联创建
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录名,相对路径:如/FTF</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool CreateFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.Timeout = 2000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.PathnameCreated)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
if (FtpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
//文件已存在,返回True
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
}
/**//// <summary>
/// 创建文件夹:实现级联创建
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录名,相对路径:如/FTF/FTF1/FTF2/FTF3</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool CreateFtpListDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
string[] AimUriArray = AimUriStr.TrimStart('/').Split('/');
string AimUriCache = string.Empty;
for (int i = 0; i < AimUriArray.Length; i++)
{
AimUriCache += "/" + AimUriArray[i];
if (CreateFtpDirectory(BaseUriStr, AimUriCache, UserName, UserPwd))
{
continue;
}
else
{
if (CreateFtpDirectory(BaseUriStr, AimUriCache, UserName, UserPwd))
{
continue;
}
else
{
return false;
}
}
}
return true;
}
#endregion
删除文件夹#region 删除文件夹
/**//// <summary>
/// 删除文件夹,不实现级联删除
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTFDEL</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool DeleteFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.Timeout = 2000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.FileActionOK)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
//如果返回信息表示文件不可操作或不存在,表明文件夹已经被删除
if (FtpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
FtpResponse.Close();
return true;
}
else
{
//返回其他错误:可能出现问题
FtpResponse.Close();
return false;
}
}
}
/**//// <summary>
/// 删除文件夹,实现级联删除
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTFDEL</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool DeleteFtpListDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
List<string> DirectoryDetailList = ListFtpDirectory(BaseUriStr, AimUriStr, UserName, UserPwd);
foreach (string ListDetail in DirectoryDetailList)
{
if (ListDetail.EndsWith("|D"))
{
//删除文件夹内容
if (ListFtpDirectory(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd).Count == 0)
{
if (!DeleteFtpDirectory(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd))
{
return false;
}
}
else
{
if (!DeleteFtpListDirectory(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd))
{
return false;
}
}
}
if (ListDetail.EndsWith("|F"))
{
//删除文件
if (!DeleteFtpFile(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd))
{
return false;
}
}
}
//删除当前文件夹
if (!DeleteFtpDirectory(BaseUriStr, AimUriStr, UserName, UserPwd))
{
return false;
}
return true;
}
#endregion
获取文件夹内文件和文件夹列表信息#region 获取文件夹内文件和文件夹列表信息
/**//// <summary>
/// 获取文件夹内文件信息
/// </summary>
/// <param name="BaseUriStr">基Uri:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTF1</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static List<string> ListFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
StreamReader srd = new StreamReader(FtpResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string ResponseBackStr = srd.ReadToEnd();
srd.Close();
FtpResponse.Close();
string[] ListDetails = ResponseBackStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> RtnList = new List<string>();
foreach (string ListDetail in ListDetails)
{
if (ListDetail.StartsWith("d") && (!ListDetail.EndsWith(".")))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|D");
}
else if (ListDetail.StartsWith("-"))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|F");
}
}
return RtnList;
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close();
return new List<string>();
}
}
#endregion
删除指定文件#region 删除指定文件
/**//// <summary>
/// 删除指定文件
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTFDEL</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool DeleteFtpFile(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.Timeout = 2000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.FileActionOK)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
if (FtpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
}
#endregion
上传文件#region 上传文件
/**//// <summary>
/// 上传文件到指定位置
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <param name="SrcFilePath">源文件位置</param>
/// <returns></returns>
public static bool UploadFtpFile(string BaseUriStr, string AimUriStr, string UserName, string UserPwd, string SrcFilePath)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.UploadFile;
try
{
Stream stm = FtpRequest.GetRequestStream();
byte[] RequestByteArray = File.ReadAllBytes(SrcFilePath);
stm.Write(RequestByteArray, 0, RequestByteArray.Length);
stm.Close();
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.ClosingData)
{
FtpResponse.Close();
FileInfo FI = new FileInfo(SrcFilePath);
if (GetFtpFileSize(BaseUriStr, AimUriStr, UserName, UserPwd) == FI.Length)
{
return true;
}
else
{
return false;
}
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close();
return false;
}
}
/**//// <summary>
/// 获取文件尺寸
/// 返回文件字节数
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static int GetFtpFileSize(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
long FileSize = FtpResponse.ContentLength;
FtpResponse.Close();
return Convert.ToInt32(FileSize);
}
catch
{
return -1;
}
}
#endregion
文件重命名#region 文件重命名
/**//// <summary>
/// 文件重命名、文件移动
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="SrcUriStr">源位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="AimUriStr">目标位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool RenameFtpFile(string BaseUriStr, string SrcUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, SrcUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.Rename;
FtpRequest.RenameTo = AimUriStr;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.FileActionOK)
{
return true;
}
else
{
return false;
}
}
catch(WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close();
return false;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.DirectoryServices.Protocols;
namespace Vancl.FileUtility
{
public class FtpCmd
{
创建文件夹#region 创建文件夹
/**//// <summary>
/// 创建文件夹:不实现级联创建
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录名,相对路径:如/FTF</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool CreateFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.Timeout = 2000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.PathnameCreated)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
if (FtpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
//文件已存在,返回True
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
}
/**//// <summary>
/// 创建文件夹:实现级联创建
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录名,相对路径:如/FTF/FTF1/FTF2/FTF3</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool CreateFtpListDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
string[] AimUriArray = AimUriStr.TrimStart('/').Split('/');
string AimUriCache = string.Empty;
for (int i = 0; i < AimUriArray.Length; i++)
{
AimUriCache += "/" + AimUriArray[i];
if (CreateFtpDirectory(BaseUriStr, AimUriCache, UserName, UserPwd))
{
continue;
}
else
{
if (CreateFtpDirectory(BaseUriStr, AimUriCache, UserName, UserPwd))
{
continue;
}
else
{
return false;
}
}
}
return true;
}
#endregion
删除文件夹#region 删除文件夹
/**//// <summary>
/// 删除文件夹,不实现级联删除
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTFDEL</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool DeleteFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.Timeout = 2000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.FileActionOK)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
//如果返回信息表示文件不可操作或不存在,表明文件夹已经被删除
if (FtpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
FtpResponse.Close();
return true;
}
else
{
//返回其他错误:可能出现问题
FtpResponse.Close();
return false;
}
}
}
/**//// <summary>
/// 删除文件夹,实现级联删除
/// 返回:true成功,false失败
/// </summary>
/// <param name="BaseUriStr">基Uri字符串:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTFDEL</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool DeleteFtpListDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
List<string> DirectoryDetailList = ListFtpDirectory(BaseUriStr, AimUriStr, UserName, UserPwd);
foreach (string ListDetail in DirectoryDetailList)
{
if (ListDetail.EndsWith("|D"))
{
//删除文件夹内容
if (ListFtpDirectory(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd).Count == 0)
{
if (!DeleteFtpDirectory(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd))
{
return false;
}
}
else
{
if (!DeleteFtpListDirectory(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd))
{
return false;
}
}
}
if (ListDetail.EndsWith("|F"))
{
//删除文件
if (!DeleteFtpFile(BaseUriStr, AimUriStr + "/" + ListDetail.Split('|')[0], UserName, UserPwd))
{
return false;
}
}
}
//删除当前文件夹
if (!DeleteFtpDirectory(BaseUriStr, AimUriStr, UserName, UserPwd))
{
return false;
}
return true;
}
#endregion
获取文件夹内文件和文件夹列表信息#region 获取文件夹内文件和文件夹列表信息
/**//// <summary>
/// 获取文件夹内文件信息
/// </summary>
/// <param name="BaseUriStr">基Uri:如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTF1</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static List<string> ListFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
StreamReader srd = new StreamReader(FtpResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string ResponseBackStr = srd.ReadToEnd();
srd.Close();
FtpResponse.Close();
string[] ListDetails = ResponseBackStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> RtnList = new List<string>();
foreach (string ListDetail in ListDetails)
{
if (ListDetail.StartsWith("d") && (!ListDetail.EndsWith(".")))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|D");
}
else if (ListDetail.StartsWith("-"))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|F");
}
}
return RtnList;
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close();
return new List<string>();
}
}
#endregion
删除指定文件#region 删除指定文件
/**//// <summary>
/// 删除指定文件
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标目录,相对路径:如/FTF/FTFDEL</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool DeleteFtpFile(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.Timeout = 2000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.FileActionOK)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
if (FtpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
FtpResponse.Close();
return true;
}
else
{
FtpResponse.Close();
return false;
}
}
}
#endregion
上传文件#region 上传文件
/**//// <summary>
/// 上传文件到指定位置
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <param name="SrcFilePath">源文件位置</param>
/// <returns></returns>
public static bool UploadFtpFile(string BaseUriStr, string AimUriStr, string UserName, string UserPwd, string SrcFilePath)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.UploadFile;
try
{
Stream stm = FtpRequest.GetRequestStream();
byte[] RequestByteArray = File.ReadAllBytes(SrcFilePath);
stm.Write(RequestByteArray, 0, RequestByteArray.Length);
stm.Close();
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.ClosingData)
{
FtpResponse.Close();
FileInfo FI = new FileInfo(SrcFilePath);
if (GetFtpFileSize(BaseUriStr, AimUriStr, UserName, UserPwd) == FI.Length)
{
return true;
}
else
{
return false;
}
}
else
{
FtpResponse.Close();
return false;
}
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close();
return false;
}
}
/**//// <summary>
/// 获取文件尺寸
/// 返回文件字节数
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="AimUriStr">目标位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static int GetFtpFileSize(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
long FileSize = FtpResponse.ContentLength;
FtpResponse.Close();
return Convert.ToInt32(FileSize);
}
catch
{
return -1;
}
}
#endregion
文件重命名#region 文件重命名
/**//// <summary>
/// 文件重命名、文件移动
/// </summary>
/// <param name="BaseUriStr">基Uri字符串,如ftp://192.168.1.190:21</param>
/// <param name="SrcUriStr">源位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="AimUriStr">目标位置,相对路径:如/FTF/FTFDEL/K.pdf</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static bool RenameFtpFile(string BaseUriStr, string SrcUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, SrcUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
FtpRequest.KeepAlive = true;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.Rename;
FtpRequest.RenameTo = AimUriStr;
try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
if (FtpResponse.StatusCode == FtpStatusCode.FileActionOK)
{
return true;
}
else
{
return false;
}
}
catch(WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close();
return false;
}
}
#endregion
}
}