【发布时间】:2013-01-16 03:44:15
【问题描述】:
我正在检查我的 FTP 服务器上是否存在目录:
public bool DirectoryExists(string directory)
{
bool directoryExists;
var request = (FtpWebRequest)WebRequest.Create(directory);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("user", "pass");
try
{
using (request.GetResponse())
{
directoryExists = true;
}
}
catch (WebException)
{
directoryExists = false;
}
return directoryExists;
}
在这种情况下:
directory = @"ftp://ftp.example.com/Rubicon";
在我的服务器上,我有一个名为 Rubicon1 的文件夹。这导致我的支票返回true。除非它与目录名称完全匹配,否则如何确保它失败?
【问题讨论】:
-
你能得到父目录的列表吗?也许它被隐藏了
-
看这个:stackoverflow.com/questions/265953/… 推理适用于任何易失性资源,其中肯定包括 FTP 共享。
-
@ericosg 上面写着
Create的那一行是用于创建 FTP Web 请求,而不是实际的 FTP 目录 -
刚刚用我的 FTP 试了一下,它按预期工作。会不会又是their fault? :)
-
@w0lf,谢谢,我知道这一点,但我的表述很糟糕。
标签: c# ftp ftpwebrequest