C# ftp 如何判断服务器上的文件夹是否存在 当有文件夹重复时,新那会出错,我如何判断这个文件夹是否存在呢,我知道一个是新那时添加异常处理,有没有其它好的办法呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; } WebRequestMethods.Ftp.ListDirectory获取FTP请求的响应流中的字符串 本来想用ListDirectory,然后分析一下后没时间研究我也用异常处理解决的期待更好的办法 vb.net 转C#的问题 救急!!! 怎么隐藏 菜单中的一个子菜单 c# 读EXCELL 表格问题(有空值) 请问一个C# 数据类型的问题,请大家帮忙解决. 两个datetable以主字段合并 请教:DevExpress的XtraTreeList控件大量数据刷新问题! 连接数据库的一点疑问? 在C#中如何对剪切板进行操作? 面向对象编程问题 Console.Write 在控制台无输出 怎么回事求救 C#中不用WebBrowser控件是否可以获得THML源代码?
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;
}
获取FTP请求的响应流中的字符串
后没时间研究
我也用异常处理解决的期待更好的办法