我有这个 FTP 方法来检查目录是否存在。它在开始时工作正常,但现在,即使目录不存在,它仍然返回 true。我尝试了很多东西,并设置了断点来查看我可以使用响应对象的哪些属性来确定目录是否存在。我还搜索了互联网,解决方案似乎对我不起作用。这是我的FTP方法。
public bool directoryExists(string directory)
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
try
{
using (ftpRequest.GetResponse())
{
return true;
}
//var response = ftpRequest.GetResponse();
//if (response != null)
// return true;
//else return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
/* Resource Cleanup */
finally
{
ftpRequest = null;
}
}
这是调用它并返回 true 的方法,即使该目录不存在:
private string getDirectory(ref FtpClass ftp, string internalID)
{
string remoteSubPathDel = internalID + "\\trunk\\prod\\xml\\delete";
string remoteSubPathUpdate = internalID + "\\trunk\\prod\\xml\\update";
string remoteSubPathNew = internalID + "\\trunk\\prod\\xml\\new";
if (ftp.directoryExists(remoteSubPathDel))
return remoteSubPathDel;
else if (ftp.directoryExists(remoteSubPathUpdate))
return remoteSubPathUpdate;
else if (ftp.directoryExists(remoteSubPathNew))
return remoteSubPathNew;
else
return String.Empty;
}
希望有人可以提供帮助。谢谢!:)