檢查

2013-01-31 45 views
10

我運行一個檢查,看看我的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。我如何確保它失敗,除非它完全匹配目錄名稱?

+1

你可以在父目錄的名單?也許它是隱藏的 – ericosg

+0

看到這個:http://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net/265958#265958推理適用於任何易失資源,它絕對包含FTP共享。 –

+0

@ericosg它說'創建'是用於創建FTP Web請求的行,而不是實際的FTP目錄 – GolfWolf

回答

6

我成功地解決了通過改變我的目錄是這個問題:

directory = @"ftp://ftp.example.com/Rubicon/";