检查FTP服务器上是否存在目录 [英] Check if directory exists on FTP server
本文介绍了检查FTP服务器上是否存在目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public bool DirectoryExists(字符串目录)
{
bool directoryExists;
var request =(FtpWebRequest)WebRequest.Create(directory);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(user,pass);
尝试
{
using(request.GetResponse())
{
directoryExists = true;
}
}
catch(WebException)
{
directoryExists = false;
}
return directoryExists;
$ / code>
在这种情况下:
directory = @ftp://ftp.example.com/Rubicon;
在我的服务器上,我有一个名为 Rubicon1 $ c $的文件夹C>。这导致我的检查返回
true
。我如何确保它失败,除非它完全匹配目录名称?
解决方案
通过更改我的目录,我成功地解决了这个问题:
directory = @ftp://ftp.example.com/Rubicon/;
I'm running a check to see if a directory exists on my FTP server:
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;
}
In this case:
directory = @"ftp://ftp.example.com/Rubicon";
On my server, I have a folder named Rubicon1
. This is causing my check to return true
. How can I ensure that it fails unless it matches the directory name exactly?
解决方案
I successfully solved this issue by changing my directory to be:
directory = @"ftp://ftp.example.com/Rubicon/";
这篇关于检查FTP服务器上是否存在目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文