FTP判断目录是否存在的方法没有直接方法,只能使用间接的方法。
使用WebRequestMethods.Ftp.ListDirectory来判断,但这个方法存在一定的问题,就是如果这个目录存在,但此目录下既没有目录,也没有文件,则这个方法就有问题。
private static bool DirectoryExist(FtpInfo info, string dirName, bool KeepAlive)
{
//修复iis设置为framework4.0的时候,ftp目录不存在时返回还是true的问题。
try
{
FtpWebRequest reqFTP = GetFtpWebRequest(info, dirName, KeepAlive);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string line = reader.ReadLine();
if (line == null)
{
LogInfo.Error("目录不存在 FTP ListDirectory:dirName=" + dirName + ",line=" + line);
return false;
}
else
{
LogInfo.Info("FTP ListDirectory:dirName=" + dirName + ",line=" + line);
}
reader.Close();
response.Close();
return true;
}
catch (Exception ex)
{
LogInfo.Error("目录异常 FTP ListDirectoryDetails:url=" + dirName, ex);
return false;
}
}
private static FtpWebRequest GetFtpWebRequest(FtpInfo info, string url, bool KeepAlive)
{
string username = info.userName;
string password = info.passWord;
try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
request.UseBinary = true;
request.KeepAlive = KeepAlive;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = !info.transferType;//配置的定义和属性的定义刚好相反
//如果客户端应用程序的数据传输过程侦听数据端口上的连接,则为 false;如果客户端应在数据端口上启动连接,则为 true。 默认值是 true
LogInfo.Debug("FTP上传文件:UsePassive=" + request.UsePassive);
if (info.ftps)
{
request.Proxy = null;
request.EnableSsl = true;//FTPS需要配置
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
}
return request;
}
catch (Exception e)
{
LogInfo.Error("FTP异常:url=" + url + "," + info, e);
throw e;
}
}
/// <summary>
/// 把响应流转换为文本。
/// </summary>
/// <param name="rsp">响应流对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>响应文本</returns>
private static string GetResponseAsString(FtpWebResponse rsp)
{
System.IO.Stream stream = null;
StreamReader reader = null;
try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
reader = new StreamReader(stream);
return reader.ReadToEnd();
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
}
但这个方法可以改进,判断父目录下是否存在指定的目录就可以改进
/// <summary>
/// 判断父目录下是否存在当前目录,如果存在当前目录,在返回true
/// </summary>
/// <param name="info"></param>
/// <param name="ParentdirName"></param>
/// <param name="KeepAlive"></param>
/// <param name="currentFolderName"></param>
/// <returns></returns>
private static bool DirectoryExist(FtpInfo info, string ParentdirName, bool KeepAlive, string currentFolderName)
{
//修复iis设置为framework4.0的时候,ftp目录不存在时返回还是true的问题。
try
{
FtpWebRequest reqFTP = GetFtpWebRequest(info, ParentdirName, KeepAlive);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string line = reader.ReadToEnd();
if (line == null)
{
LogInfo.Error("目录不存在 FTP ListDirectory:ParentdirName=" + ParentdirName + ",currentFolderName = " + currentFolderName+", line=" + line);
return false;
}
else
{
LogInfo.Info("FTP ListDirectory ParentdirName=" + ParentdirName + ",currentFolderName = " + currentFolderName + ", line=" + line);
if (line.IndexOf(currentFolderName) >=0)
{
return true;
}
LogInfo.Error("目录不存在 FTP ParentdirName=" + ParentdirName + ",currentFolderName = " + currentFolderName + ", line=" + line);
}
reader.Close();
response.Close();
return false;
}
catch (Exception ex)
{
LogInfo.Error("目录异常 FTP ListDirectoryDetails:url=" + ParentdirName, ex);
return false;
}
}