I need to know whether a particular file exists on a given FTP server when I run a job that calls the said SSIS package. Currently I'm employing a script task(within SSIS) that subsumes the following code:
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;
However, this is calling the WebRequestMethods.Ftp.DownloadFile
method, which according to the MSDN website is used to is used to download a file from an FTP server. I just need to know if the file exists, not download it
.
Also, is there a better way to check for file existence?
PS. I'm new to C#, so I'm sorry if sound naive!