2016-05-17 92 views
0

當我運行一個調用上述SSIS包的作業時,我需要知道給定的FTP服務器上是否存在特定的文件。目前我使用的是涵括以下代碼腳本任務(SSIS中):通過SSIS檢查FTP服務器上是否存在文件的最佳方法?

 string userName = Dts.Variables["User::ftp_username"].Value.ToString(); 
     string password = Dts.Variables["User::ftp_password"].Value.ToString(); 
     string fileName = Dts.Variables["User::download_file_name"].Value.ToString(); 
     string ftpURL = String.Format("ftp://abc.def.com/{0}", fileName); 

     try 
     { 
      FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL); 
      ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
      ftpRequest.Credentials = new NetworkCredential(userName, password); 

      using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) 
      { 
       Dts.Variables["User::fileExists"].Value = true; 
      } 
     } 
     catch 
     { 
      Dts.Variables["User::fileExists"].Value = false; 
     } 

     Dts.TaskResult = (int)ScriptResults.Success; 

然而,這是調用WebRequestMethods.Ftp.DownloadFile方法,它根據MSDN網站是用來被用於從下載文件一個FTP服務器。 I just need to know if the file exists, not download it

此外,有沒有更好的方法來檢查文件的存在?

PS。我是C#的新手,所以我很抱歉,如果天真!

回答

0

我會完全刪除該腳本,並使用內置於SSIS中的FTP任務。

發送一個文件,其中覆蓋設置爲false,如果它已經存在,則相應地處理錯誤 - 即FTP失敗。

如果FTP成功(即該文件不存在!) - 不要忘記刪除它!

相關問題