如何检查FTP目录是否存在

寻找通过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。 有人能指出我正确的方向。

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

 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; } } } 

我假设您已经熟悉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. } } } 

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

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

并且在目录不存在的情况下等待exception。 这个方法没有抛出exception。

经过几次点击和试验后,我将目录从“ ftp://ftpserver.com/rootdir/test_if_exist_directory ”更改为:“ 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); 

我会尝试这样的方法:

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

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

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

这基本上是当前版本的Rebex FTP组件中的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(); 

用户这个代码可能是你的答案..

  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); 

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

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

所以我提出的方法是尝试上传到该文件夹​​。 但是,一个’gotcha’是你可以在这个postUploading 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; } 

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

我无法得到@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" 

然后这开始工作。

对我有用的唯一方法是通过尝试创建目录/路径(如果它已经存在将抛出exception)来反转逻辑,如果是,则之后再次删除它。 否则,使用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是我用于此的方法。 所有其他解决方案对我不起作用。

希望能帮助到你

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