C#中如何检测ftp是否存在
2个回答
展开全部
摘自:http://blog.csdn.net/hejialin666/article/details/3522815
C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
只摘录其中一个,未检查是否正确(我这边没有这个条件),仅供你参考:
/// <summary>
/// ftp文件存在检查
/// </summary>
/// <param name="ftpPath"></param>
/// <param name="ftpName"></param>
/// <returns></returns>
public bool fileCheckExist(string ftpPath, string ftpName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
WebResponse webResponse = null;
StreamReader reader = null;
try
{
string url = ftpRootURL + ftpPath;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.KeepAlive = false;
webResponse = ftpWebRequest.GetResponse();
reader = new StreamReader(webResponse.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (line == ftpName)
{
success = true;
break;
}
line = reader.ReadLine();
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (webResponse != null)
{
webResponse.Close();
}
}
return success;
}
}
C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
只摘录其中一个,未检查是否正确(我这边没有这个条件),仅供你参考:
/// <summary>
/// ftp文件存在检查
/// </summary>
/// <param name="ftpPath"></param>
/// <param name="ftpName"></param>
/// <returns></returns>
public bool fileCheckExist(string ftpPath, string ftpName)
{
bool success = false;
FtpWebRequest ftpWebRequest = null;
WebResponse webResponse = null;
StreamReader reader = null;
try
{
string url = ftpRootURL + ftpPath;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.KeepAlive = false;
webResponse = ftpWebRequest.GetResponse();
reader = new StreamReader(webResponse.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
if (line == ftpName)
{
success = true;
break;
}
line = reader.ReadLine();
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (webResponse != null)
{
webResponse.Close();
}
}
return success;
}
}
2011-12-30
展开全部
提问有异议,是判断是否启用了FTP 还是FTP内的文件是否存在。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询