110,536
社区成员
发帖
与我相关
我的任务
分享
public bool CheckFileExist(string ftpFilePath)
{
FtpWebRequest ftpWebRequest = null;
WebResponse webResponse = null;
StreamReader reader = null;
try
{
int s = ftpFilePath.LastIndexOf('/');
if (s == ftpFilePath.Length - 1)
{
ftpFilePath = ftpFilePath.Substring(0, ftpFilePath.Length - 1);
s = ftpFilePath.LastIndexOf('/');
}
string ftpFileName = ftpFilePath.Substring(s + 1, ftpFilePath.Length - s - 1);
string uri = GetFtpUri(ftpHostAddress, ftpPort, ftpFilePath.Substring(0, s + 1));
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.UsePassive = false;
ftpWebRequest.KeepAlive = false;
webResponse = ftpWebRequest.GetResponse();
reader = new StreamReader(webResponse.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (line == ftpFileName)
{
return true;
}
line = reader.ReadLine();
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (webResponse != null)
{
webResponse.Close();
}
}
return false;
}