2010-01-05 27 views
1

如何检查服务器上是否存在某个DIR? 虽然我可以检查文件是否存在或不通过:Dir存在或不存在于FTP上

try 
{ 
    FtpWebRequest request=null; 

    request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl + "/somefile.txt"); 
    request.Credentials = new NetworkCredential(username, password); 
    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) 
     { 
      //task 
     } 
    } 
} 

但我怎么检查DIR?如果我只在URI中指定DIR,那么在DIR不存在的情况下它不会被捕获。

+0

令人印象深刻,11个问题,你从来没有接受一个答案...... – Bobby 2010-01-05 09:11:56

+0

虽然直到我没有得到任何确切的答案,我需要他们操纵它们,所以我没有将它们标记为答案,因为其他用户(正在搜索某个查询)将从正确答案转移。 – marshalprince 2010-01-05 09:23:41

+0

@marshalprince这位先生,没有任何意义。 – Dan 2012-10-03 02:36:03

回答

0

我不认为代码做你认为它的作用。 就我所了解的文档而言,您试图获得文件的ls(在DOS/Windows中为dir,文件在目录中的列表)。这没有意义。它在某种程度上起作用,因为您遇到了尝试访问目录“somefile.txt”的例外情况。

您应该能够通过观察父母的ListDirectory响应的输出做正确的方式(TM):

做一个ListDirectory ftp://yourserver/并检查是否

  • 文件
  • 您的目录

已上市。

+0

,那么如果我需要去一些dir然后为什么我没有发现代码中的任何异常。 like ftp://someip.com/folder/folder/ 如果文件夹目录中没有文件夹,则此选项不会返回任何异常。 – marshalprince 2010-01-05 09:26:24

+0

好的。这是一种方式,但会在代码中产生额外的开销。为什么回应不会成为空白,或者如果它不在那里就会引发例外。 – marshalprince 2010-01-05 09:48:09

2
request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl); //no file name 
request.Credentials = new NetworkCredential(username, password); 
myFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory; 

并检查您的文件/目录是否列出。

您需要询问响应,它应该包含可能的文件和导演列表。

您不应该使用catch来处理程序流。 MSDN example

0

我用:

private bool CreateFTPDirectory(string directory) 
{ 

    try 
    { 
     //create the directory 
     FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpURI+"/"+directory)); 
     requestDir.Method = WebRequestMethods.Ftp.MakeDirectory; 
     requestDir.UsePassive = true; 
     requestDir.UseBinary = true; 
     requestDir.KeepAlive = false; 
     //requestDir.UseDefaultCredentials = true; 
     requestDir.Credentials = new NetworkCredential(UserId, Password); 
     requestDir.Proxy = WebRequest.DefaultWebProxy; 
     requestDir.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 

     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) || (((int)response.StatusCode)==521)) 
     { 
      response.Close(); 
      return true; 
     } 
     else 
     { 
      response.Close(); 
      return false; 
     } 
    } 
} 

这有创建目录以及副作用。如果它已经存在,你会得到一个返回的521结果,这在.NET Enum中没有定义。

当您连接到FTP服务器时,您可能指定Uri为“ftp // ftp.domain.com/somedirectory”,但这转换为:“ftp://ftp.domain.com/homedirectoryforftp/somedirectory”。为了能够定义完整的根目录,使用“ftp://ftp.domain.com//somedirectory”,它转换为计算机上的somedirectory。

0

通过使用edtFTPnet

private void ftp_folder_IsExists() 
{ 
    FTPClient ftp = default(FTPClient); 

    ftp = new FTPClient("ftp_host_name_here"); // ex.:- no need to use ftp in the host name, provide name only 
    ftp.Login("username", "password"); 

    string[] file_and_folders = ftp.Dir(".", false);// . is used to get all the [files and folders] in the root of FTP 

    string[] file_and_folders_1 = ftp.Dir("MyFolder", false);// this will get all the [files and folder] inside MyFolder (ex. ftp.ahostname.com/MyFolder/) 


    //checking for a FILE 
    if (file_and_folders.Contains("something.txt")) { 
     //Do what you want.. 
    } else { 
     //Do what you want.. 

    } 

    //checking for a FOLDER 
    if (file_and_folders.Contains("A_Folder")) { 
     //Do what you want.. 
    } else { 
     //Do what you want.. 

    } 

} 

注:代码写在VB.NET和转化利用http://converter.telerik.com/