如何检查FTP目录是否存在

时间:2010-05-04 21:35:11

标签: c# ftp ftpwebrequest

寻找通过FTP检查给定目录的最佳方法。

目前我有以下代码:

private bool FtpDirectoryExists(string directory, string username, string password)
{

    try
    {
        var request = (FtpWebRequest)WebRequest.Create(directory);
        request.Credentials = new NetworkCredential(username, password);
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
            return false;
        else
            return true;
    }
    return true;
}

无论目录是否存在,都返回false。有人能指出我正确的方向。

11 个答案:

答案 0 :(得分:18)

基本上困住了我在创建目录时收到的错误。

private bool CreateFTPDirectory(string directory) {

    try
    {
        //create the directory
        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));
        requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
        requestDir.Credentials = new NetworkCredential("username", "password");
        requestDir.UsePassive = true;
        requestDir.UseBinary = true;
        requestDir.KeepAlive = false;
        FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
        Stream ftpStream = response.GetResponseStream();

        ftpStream.Close();
        response.Close();

        return true;
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
        {
            response.Close();
            return true;
        }
        else
        {
            response.Close();
            return false;
        }  
    }
}

答案 1 :(得分:8)

我认为您已经熟悉FtpWebRequest,因为这是在.NET中访问FTP的常用方法。

您可以尝试列出目录并检查错误StatusCode。

    try 
{  
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.microsoft.com/12345");  
    request.Method = WebRequestMethods.Ftp.ListDirectory;  
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
    {  
        // Okay.  
    }  
}  
catch (WebException ex)  
{  
    if (ex.Response != null)  
    {  
        FtpWebResponse response = (FtpWebResponse)ex.Response;  
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)  
        {  
            // Directory not found.  
        }  
    }  
} 

答案 2 :(得分:7)

我也遇到了类似的问题。我正在使用,

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpserver.com/rootdir/test_if_exist_directory");  
request.Method = WebRequestMethods.Ftp.ListDirectory;  
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

并在目录不存在的情况下等待例外。这种方法没有抛出异常。

经过几次点击和试验,我改变了目录:  " ftp://ftpserver.com/rootdir/test_if_exist_directory" to:" ftp://ftpserver.com/rootdir/test_if_exist_directory/"。现在这件作品对我有用。

我认为我们应该在ftp文件夹的uri中附加反斜杠(/)以使其工作。

根据要求,完整的解决方案现在将是:

public bool DoesFtpDirectoryExist(string dirPath)
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);  
        request.Method = WebRequestMethods.Ftp.ListDirectory;  
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        return true;
     }
     catch(WebException ex)
     {
         return false;
     }
}

//Calling the method:
string ftpDirectory = "ftp://ftpserver.com/rootdir/test_if_exist_directory/"; //Note: backslash at the last position of the path.
bool dirExists = DoesFtpDirectoryExist(ftpDirectory);

答案 3 :(得分:6)

我会尝试这样做:

  • 发送MLST<目录> FTP命令(在RFC3659中定义)并解析它的输出。它应返回包含现有目录的目录详细信息的有效行。

  • 如果MLST命令不可用,请尝试使用CWD命令将工作目录更改为测试目录。在更改为经过测试的目录之前,不要忘记确定当前路径(PWD命令)以便能够返回。

  • 在某些服务器上,MDTM和SIZE命令的组合可用于类似目的,但行为非常复杂,超出了本文的范围。

这基本上是我们Rebex FTP component当前版本中的DirectoryExists方法。以下代码显示了如何使用它:

string path = "/path/to/directory";

Rebex.Net.Ftp ftp = new Rebex.Net.Ftp();
ftp.Connect("hostname");
ftp.Login("username","password");

Console.WriteLine(
  "Directory '{0}' exists: {1}", 
  path, 
  ftp.DirectoryExists(path)
);

ftp.Disconnect();

答案 4 :(得分:4)

使用此代码可能是您的答案..

 public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
        {
            bool IsExists = true;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                IsExists = false;
            }
            return IsExists;
        }

我将此方法称为:

bool result =    FtpActions.Default.FtpDirectoryExists( @"ftp://mydomain.com/abcdir", txtUsername.Text, txtPassword.Text);

为什么要使用另一个库 - 创建自己的逻辑。

答案 5 :(得分:2)

我尝试了每种方法来获得可靠的检查,但WebRequestMethods.Ftp.PrintWorkingDirectoryWebRequestMethods.Ftp.ListDirectory方法都无法正常工作。检查ftp://<website>/Logs时,它们在服务器上不存在但是他们说它确实存在时失败了。

所以我提出的方法是尝试上传到该文件夹​​。但是,一个'gotcha'是您可以在此帖子Uploading to Linux

中阅读的路径格式

以下是代码段

private bool DirectoryExists(string d) 
{ 
    bool exists = true; 
    try 
    { 
        string file = "directoryexists.test"; 
        string path = url + homepath + d + "/" + file;
        //eg ftp://website//home/directory1/directoryexists.test
        //Note the double space before the home is not a mistake

        //Try to save to the directory 
        req = (FtpWebRequest)WebRequest.Create(path); 
        req.ConnectionGroupName = "conngroup1"; 
        req.Method = WebRequestMethods.Ftp.UploadFile; 
        if (nc != null) req.Credentials = nc; 
        if (cbSSL.Checked) req.EnableSsl = true; 
        req.Timeout = 10000; 

        byte[] fileContents = System.Text.Encoding.Unicode.GetBytes("SAFE TO DELETE"); 
        req.ContentLength = fileContents.Length; 

        Stream s = req.GetRequestStream(); 
        s.Write(fileContents, 0, fileContents.Length); 
        s.Close(); 

        //Delete file if successful 
        req = (FtpWebRequest)WebRequest.Create(path); 
        req.ConnectionGroupName = "conngroup1"; 
        req.Method = WebRequestMethods.Ftp.DeleteFile; 
        if (nc != null) req.Credentials = nc; 
        if (cbSSL.Checked) req.EnableSsl = true; 
        req.Timeout = 10000; 

        res = (FtpWebResponse)req.GetResponse(); 
        res.Close(); 
    } 
    catch (WebException ex) 
    { 
        exists = false; 
    } 
    return exists; 
} 

答案 6 :(得分:0)

导航到父目录,执行“ls”命令,然后解析结果。

答案 7 :(得分:0)

我无法得到@BillyLogans的建议......

我发现问题是默认的FTP目录是/ home / usr / fred

我用的时候:

String directory = "ftp://some.domain.com/mydirectory"
FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));

我发现这变成了

"ftp:/some.domain.com/home/usr/fred/mydirectory"

停止此操作将目录Uri更改为:

String directory = "ftp://some.domain.com//mydirectory"

然后开始工作。

答案 8 :(得分:-1)

这是我最好的。从父目录获取列表,并检查父目录是否具有正确的子名称

public void TryConnectFtp(string ftpPath)
        {
            string[] splited = ftpPath.Split('/');
            StringBuilder stb = new StringBuilder();
            for (int i = 0; i < splited.Length - 1; i++)
            {
                stb.Append(splited[i] +'/');
            }
            string parent = stb.ToString();
            string child = splited.Last();

            FtpWebRequest testConnect = (FtpWebRequest)WebRequest.Create(parent);
            testConnect.Method = WebRequestMethods.Ftp.ListDirectory;
            testConnect.Credentials = credentials;
            using (FtpWebResponse resFtp = (FtpWebResponse)testConnect.GetResponse())
            {
                StreamReader reader = new StreamReader(resFtp.GetResponseStream());
                string result = reader.ReadToEnd();
                if (!result.Contains(child) ) throw new Exception("@@@");

                resFtp.Close();
            }
        }

答案 9 :(得分:-2)

对我来说唯一有效的方法是通过尝试创建目录/路径(如果它已经存在则会抛出异常)来反转逻辑,如果是,那么之后再次删除它。否则,使用Exception设置一个标记目录/路径存在的标志。我对VB.NET很陌生,我觉得有一种更好的方法来编写代码 - 但无论如何这里是我的代码:

        Public Function DirectoryExists(directory As String) As Boolean
        ' Reversed Logic to check if a Directory exists on FTP-Server by creating the Directory/Path
        ' which will throw an exception if the Directory already exists. Otherwise create and delete the Directory

        ' Adjust Paths
        Dim path As String
        If directory.Contains("/") Then
            path = AdjustDir(directory)     'ensure that path starts with a slash
        Else
            path = directory
        End If

        ' Set URI (formatted as ftp://host.xxx/path)

        Dim URI As String = Me.Hostname & path

        Dim response As FtpWebResponse

        Dim DirExists As Boolean = False
        Try
            Dim request As FtpWebRequest = DirectCast(WebRequest.Create(URI), FtpWebRequest)
            request.Credentials = Me.GetCredentials
            'Create Directory - if it exists WebException will be thrown
            request.Method = WebRequestMethods.Ftp.MakeDirectory

            'Delete Directory again - if above request did not throw an exception
            response = DirectCast(request.GetResponse(), FtpWebResponse)
            request = DirectCast(WebRequest.Create(URI), FtpWebRequest)
            request.Credentials = Me.GetCredentials
            request.Method = WebRequestMethods.Ftp.RemoveDirectory
            response = DirectCast(request.GetResponse(), FtpWebResponse)
            DirExists = False

        Catch ex As WebException
            DirExists = True
        End Try
        Return DirExists

    End Function

WebRequestMethods.Ftp.MakeDirectory和WebRequestMethods.Ftp.RemoveDirectory是我用于此的方法。所有其他解决方案对我不起作用。

希望有所帮助

答案 10 :(得分:-6)

对于它的价值,如果你使用EnterpriseDT's FTP组件,你将使你的FTP生活更轻松。它是免费的,可以帮助您避免头痛,因为它处理命令和响应。你只需使用一个漂亮,简单的对象。