2016-05-17 69 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成功(即该文件不存在!) - 不要忘记删除它!

相关问题