public void Connect()
{
try
{
Protocol protocol;
string ShiftedType = FtpType.ToUpper();
if (ShiftedType.Contains("SFTP"))
{
protocol = Protocol.Sftp;
}
else
{
if (ShiftedType.Contains("FTP"))
{
protocol = Protocol.Ftp;
}
else
{
if (ShiftedType.Contains("SCP"))
{
protocol = Protocol.Scp;
}
else
{
if (ShiftedType.Contains("WEBDAV"))
{
protocol = Protocol.Webdav;
}
else
{
protocol = Protocol.S3;
}
}
}
}
SessionOptions options = new SessionOptions
{
Protocol = protocol,
HostName = Server,
UserName = UserName,
Password = Password
};
if (protocol == Protocol.Sftp)
{
options.SshHostKeyFingerprint = EncryptionKey;
}
if (protocol == Protocol.Webdav)
{
options.RootPath = RemoteDirectory.StartsWith("/") ? RemoteDirectory : "/" + RemoteDirectory;
options.Secure = true;
}
else
{
options.FtpMode = FtpMode.Active;
options.PortNumber = Port;
}
TransferOptions = new TransferOptions()
{
TransferMode = TransferMode.Binary,
FilePermissions = null,
PreserveTimestamp = false
};
if(Debug)
{
Session.DebugLogLevel = 1;
Session.SessionLogPath = Path.Combine(LocalDirectory, "Session.xml");
}
Session.Open(options);
}
catch (Exception ex)
{
throw ex;
}
}
public void DoFtp()
{
try
{
if (Debug)
{
if (Session != null)
{
if (Session.Opened)
{
Session.Close();
}
}
}
LogWriter writer = new LogWriter()
{
JobName = this.JobName,
ConnectionString = this.ConnectionString,
LogMessage = "Connecting to " + Server + "..."
};
if (!Session.Opened)
{
Connect();
}
else
{
writer.LogMessage = "Session already connected. Closing...";
writer.WriteLog();
Session.Close();
writer.LogMessage = "Session closed. Connecting...";
writer.WriteLog();
Connect();
}
writer.LogMessage = "Connected";
writer.WriteLog();
RemoteFileExists = Session.FileExists(RemoteDirectory.Length > 0 ? RemoteDirectory + RemoteFile : RemoteFile);
TransferOperationResult transferOperation = null;
switch (FtpCommand.ToUpper())
{
case "GET":
FileCount = 1;
if (RemoteFileExists)
{
writer.LogMessage = "Getting File " + RemoteFile;
writer.WriteLog();
transferOperation = Session.GetFiles(RemoteFile, LocalDirectory, false, TransferOptions);
}
else
{
if (GetFileCount() == 0)
{
writer.LogMessage = "No file exists";
writer.WriteLog();
ExceptionWriter ew = new ExceptionWriter()
{
ConnectionString = writer.ConnectionString,
JobName = JobName,
ErrorCode = 50000,
ErrorDescription = "No files were available",
Source = "FTP GET",
Target = "SCR FTP"
};
ew.WriteException();
return;
}
else
{
writer.LogMessage = "Getting File " + RemoteFile;
writer.WriteLog();
transferOperation = Session.GetFiles(RemoteFile, LocalDirectory, false, TransferOptions);
}
}
break;
case "MGET":
FileCount = GetFileCount();
if (FileCount > 0)
{
writer.LogMessage = "Getting Files " + RemoteFile;
writer.WriteLog();
transferOperation = Session.GetFiles(RemoteFile, LocalDirectory, false, TransferOptions);
}
else
{
writer.LogMessage = "No file exists";
writer.WriteLog();
ExceptionWriter ew = new ExceptionWriter()
{
ConnectionString = writer.ConnectionString,
JobName = JobName,
ErrorCode = 50000,
ErrorDescription = "No files were available",
Source = "FTP MGET",
Target = "SCR FTP"
};
ew.WriteException();
return;
}
break;
case "PUT":
string[] PutFiles = Directory.GetFiles(LocalDirectory, WildCard);
FileCount = PutFiles.Length;
if (PutFiles.Length > 0)
{
writer.LogMessage = "Putting File " + LocalFile;
writer.WriteLog();
foreach (string put in PutFiles)
{
string strFileName = put.Substring(put.LastIndexOf("\\") + 1);
if (Session.FileExists(WildCard) && !AppendIfExists)
{
transferOperation = Session.PutFiles(put, RemoteDirectory.Length > 0 ? RemoteDirectory + strFileName : strFileName, false, TransferOptions);
if (RemoteDirectory.Length > 0)
{
writer.LogMessage = "Put " + put + " on " + Server + " in " + RemoteDirectory;
writer.WriteLog();
}
else
{
writer.LogMessage = "Put " + put + " on " + Server + " in /";
writer.WriteLog();
}
}
else
{
if (AppendIfExists && Session.FileExists(WildCard))
{
TransferOptions.OverwriteMode = OverwriteMode.Append;
transferOperation = Session.PutFiles(put, RemoteDirectory.Length > 0 ? RemoteDirectory + strFileName : strFileName, false, TransferOptions);
if (RemoteDirectory.Length > 0)
{
writer.LogMessage = "Appending " + put + " on " + Server + " in " + RemoteDirectory;
writer.WriteLog();
}
else
{
writer.LogMessage = "Appending " + put + " on " + Server + " in /";
writer.WriteLog();
}
}
else
{
if (!Session.FileExists(WildCard))
{
transferOperation = Session.PutFiles(put, RemoteDirectory.Length > 0 ? RemoteDirectory + strFileName : strFileName, false, TransferOptions);
if (RemoteDirectory.Length > 0)
{
writer.LogMessage = "Put " + put + " on " + Server + " in " + RemoteDirectory;
writer.WriteLog();
}
else
{
writer.LogMessage = "Put " + put + " on " + Server + " in /";
writer.WriteLog();
}
}
}
}
}
}
else
{
writer.LogMessage = "File " + LocalFile + " does not exist.";
writer.WriteLog();
ExceptionWriter ew = new ExceptionWriter()
{
ConnectionString = writer.ConnectionString,
JobName = JobName,
ErrorCode = 50000,
ErrorDescription = "No files were available",
Source = "FTP PUT",
Target = "SCR FTP"
};
ew.WriteException();
return;
}
break;
case "MPUT":
string[] FilesToPut = Directory.GetFiles(LocalDirectory, WildCard);
FileCount = FilesToPut.Length;
if (FilesToPut.Length > 0)
{
writer.LogMessage = "Putting Files " + LocalFile;
writer.WriteLog();
foreach (string put in FilesToPut)
{
string strFileName = put.Substring(put.LastIndexOf("\\") + 1);
if (Session.FileExists(strFileName) && !AppendIfExists || !Session.FileExists(strFileName))
{
transferOperation = Session.PutFiles(put, RemoteDirectory.Length > 0 ? RemoteDirectory + strFileName : strFileName, false, TransferOptions);
if (RemoteDirectory.Length > 0)
{
writer.LogMessage = "Put " + put + " on " + Server + " in " + RemoteDirectory;
writer.WriteLog();
}
else
{
writer.LogMessage = "Put " + put + " on " + Server + " in /";
writer.WriteLog();
}
}
else
{
if (!Session.FileExists(strFileName))
{
TransferOptions.OverwriteMode = OverwriteMode.Append;
transferOperation = Session.PutFiles(put, RemoteDirectory.Length > 0 ? RemoteDirectory + strFileName : strFileName, false, TransferOptions);
if (RemoteDirectory.Length > 0)
{
writer.LogMessage = "Appending " + put + " on " + Server + " in " + RemoteDirectory;
writer.WriteLog();
}
else
{
writer.LogMessage = "Appending " + put + " on " + Server + " in /";
writer.WriteLog();
}
}
}
}
}
else
{
writer.LogMessage = "File " + LocalFile + " does not exist.";
writer.WriteLog();
ExceptionWriter ew = new ExceptionWriter()
{
ConnectionString = writer.ConnectionString,
JobName = JobName,
ErrorCode = 50000,
ErrorDescription = "No files were available",
Source = "FTP MPUT",
Target = "SCR FTP"
};
ew.WriteException();
return;
}
break;
}
if (transferOperation != null)
{
transferOperation.Check();
}
writer.LogMessage = "Operation Complete";
writer.WriteLog();
string[] FileArray = Directory.GetFiles(LocalDirectory, WildCard);
string MultipleFiles = string.Empty;
if (FileArray.Length == 1)
{
MultipleFiles = " file";
}
else
{
if (FileArray.Length == 0)
{
MultipleFiles = " files";
ExceptionWriter ew = new ExceptionWriter()
{
JobName = JobName,
ErrorCode = 50000,
ErrorDescription = "No files were available",
Source = "FTP PUT",
Target = "SCR FTP"
};
return;
}
else
MultipleFiles = " files";
}
string Action = string.Empty;
if (FtpCommand.ToUpper().Contains("GET"))
{
Action = "Retrieved ";
}
else
{
Action = "Sent ";
}
writer.LogMessage = Action + FileArray.Length + " " + MultipleFiles + "...";
writer.WriteLog();
}
catch (Exception ex)
{
throw ex;
}
}
and the calling code:
string strEncryptionKey = string.Empty;
string strFtpCommand = string.Empty;
string strRemoteDirectory = string.Empty;
string strLocalDirectory = string.Empty;
string strServer = string.Empty;
string strUserName = string.Empty;
string strPassword = string.Empty;
string strFileName = string.Empty;
string strFtpType = string.Empty;
string strJobName = string.Empty;
string strLocalFile = string.Empty;
string strRemoteFile = string.Empty;
string strWildCard = string.Empty;
bool bParsed = false;
string strExecutablePath = string.Empty;
string strArchiveDirectory = string.Empty;
string strFileDate = string.Empty;
string strDateFormat = string.Empty;
string strCopyFileDestination = string.Empty;
try
{
Variables requiredVariables = null;
Dts.VariableDispenser.LockOneForRead("User::FtpPort", ref requiredVariables);
bParsed = Int16.TryParse(requiredVariables["User::FtpPort"].Value.ToString(), out Int16 nPort);
if (!bParsed)
{
nPort = 0;
}
Dts.VariableDispenser.LockOneForRead("$Package::JobName", ref requiredVariables);
strJobName = requiredVariables["$Package::JobName"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::EncryptionKey", ref requiredVariables);
strEncryptionKey = requiredVariables["User::EncryptionKey"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::FtpServer", ref requiredVariables);
strServer = requiredVariables["User::FtpServer"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::FtpCommand", ref requiredVariables);
strFtpCommand = requiredVariables["User::FtpCommand"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::FtpTypeName", ref requiredVariables);
strFtpType = requiredVariables["User::FtpTypeName"].Value.ToString();
string strFtpTypeMin = string.Empty;
strFtpTypeMin = strFtpType.Length > 4 ? strFtpType.Substring(0, 4).ToUpper() : "FTP";
Dts.VariableDispenser.LockOneForRead("User::RemoteDirectory", ref requiredVariables);
strRemoteDirectory = requiredVariables["User::RemoteDirectory"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::LocalDirectory", ref requiredVariables);
strLocalDirectory = requiredVariables["User::LocalDirectory"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::FtpUserName", ref requiredVariables);
strUserName = requiredVariables["User::FtpUserName"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::FtpPassword", ref requiredVariables);
strPassword = requiredVariables["User::FtpPassword"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::FileName", ref requiredVariables);
strFileName = requiredVariables["User::FileName"].Value.ToString();
strWildCard = strFileName.Replace("(period)", "*");
strLocalFile = strLocalDirectory.Length > 0 ? strLocalDirectory + "\\" + strFileName : strFileName;
strRemoteFile = strRemoteDirectory.Length > 0 ? strRemoteDirectory + "/" + strFileName : strFileName;
Dts.VariableDispenser.LockOneForRead("User::DeleteAfterFtp", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::DeleteAfterFtp"].Value.ToString(), out bool bDeleteAfterFTP);
bDeleteAfterFTP = bParsed && bDeleteAfterFTP;
Dts.VariableDispenser.LockOneForRead("User::ArchiveFile", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::ArchiveFile"].Value.ToString(), out bool bArchiveAfterFTP);
bArchiveAfterFTP = bParsed && bArchiveAfterFTP;
Dts.VariableDispenser.LockOneForRead("User::ArchiveDirectory", ref requiredVariables);
strArchiveDirectory = requiredVariables["User::ArchiveDirectory"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::AddDateToFile", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::AddDateToFile"].Value.ToString(), out bool bAddDateToFileName);
bAddDateToFileName = bParsed && bAddDateToFileName;
Dts.VariableDispenser.LockOneForRead("User::FtpFileDateFormat", ref requiredVariables);
strDateFormat = requiredVariables["User::FtpFileDateFormat"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::MoveFile", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::MoveFile"].Value.ToString(), out bool bMoveFilesAfterImport);
bMoveFilesAfterImport = bParsed && bMoveFilesAfterImport;
Dts.VariableDispenser.LockOneForRead("User::FileDestination", ref requiredVariables);
string strFileDestination = requiredVariables["User::FileDestination"].Value.ToString();
if (!strLocalDirectory.EndsWith("\\") && strLocalDirectory.Length > 0) strLocalDirectory += "\\";
if (!strRemoteDirectory.EndsWith("/") && strRemoteDirectory.Length > 0) strRemoteDirectory += "/";
Dts.VariableDispenser.LockOneForRead("User::ArchiveRetainDays", ref requiredVariables);
bParsed = Int16.TryParse(requiredVariables["User::ArchiveRetainDays"].Value.ToString(), out Int16 nArchiveRetainDays);
if (!bParsed)
{
nArchiveRetainDays = 0;
}
Dts.VariableDispenser.LockOneForRead("User::MaxArchiveCopies", ref requiredVariables);
bParsed = Int16.TryParse(requiredVariables["User::MaxArchiveCopies"].Value.ToString(), out Int16 nMaxArchiveCount);
if (!bParsed)
{
nMaxArchiveCount = 0;
}
Dts.VariableDispenser.LockOneForRead("User::AppendIfExists", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::AppendIfExists"].Value.ToString(), out bool bAppendIfExists);
bAppendIfExists = bParsed && bAppendIfExists;
Dts.VariableDispenser.LockOneForRead("User::CopyFile", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::CopyFile"].Value.ToString(), out bool bCopyFile);
bCopyFile = bParsed && bCopyFile;
Dts.VariableDispenser.LockOneForRead("User::CopyFileDestination", ref requiredVariables);
strCopyFileDestination = requiredVariables["User::CopyFileDestination"].Value.ToString();
Dts.VariableDispenser.LockOneForRead("User::ArchiveFileWithTimeStamp", ref requiredVariables);
bParsed = bool.TryParse(requiredVariables["User::ArchiveFileWithTimeStamp"].Value.ToString(), out bool bArchiveFileWithTimestamp);
bArchiveFileWithTimestamp = bParsed && bArchiveFileWithTimestamp;
bool bDateAdded = true;
if (strFtpCommand.ToUpper().Contains("PUT") && bAddDateToFileName)
{
LogWriter logWriter = new LogWriter()
{
ConnectionString = Dts.Connections[0].ConnectionString,
JobName = strJobName,
LogMessage = "Updating file names"
};
CExceptionRetry retry = new CExceptionRetry()
{
Function = logWriter.WriteLog,
AutoIncreaseTimeDelay = true,
IsTransient = true,
MaximumRetries = 3,
TimeDelay = 60,
JobName = strJobName,
ReportConnectionString = Dts.Connections[0].ConnectionString
};
retry.Retry();
Ftp ftpRename = new Ftp()
{
ConnectionString = logWriter.ConnectionString,
DateAdded = bDateAdded,
LocalDirectory = strLocalDirectory,
RemoteDirectory = strRemoteDirectory,
LocalFile = strLocalFile,
RemoteFile = strWildCard,
AddDateToFileName = bAddDateToFileName,
DateFormat = strDateFormat,
FileName = strFileName,
JobName = strJobName,
Server = strServer,
Port = nPort,
UserName = strUserName,
Password = strPassword,
FtpCommand = strFtpCommand,
FtpType = strFtpType,
WildCard = strWildCard,
EncryptionKey = strEncryptionKey,
ArchiveFileWithTimestamp = bArchiveFileWithTimestamp
};
retry = new CExceptionRetry()
{
AutoIncreaseTimeDelay = true,
IsTransient = true,
MaximumRetries = 3,
TimeDelay = 60,
JobName = strJobName,
ReportConnectionString = Dts.Connections[0].ConnectionString,
Function = ftpRename.AddDateToFile
};
retry.Retry();
bDateAdded = ftpRename.DateAdded;
}
LogWriter writer = new LogWriter()
{
ConnectionString = Dts.Connections[0].ConnectionString,
JobName = strJobName,
LogMessage = "Setting FTP options"
};
CExceptionRetry cExcpetionRetry = new CExceptionRetry()
{
Function = writer.WriteLog,
AutoIncreaseTimeDelay = true,
IsTransient = true,
MaximumRetries = 3,
TimeDelay = 60,
JobName = strJobName,
ReportConnectionString = Dts.Connections[0].ConnectionString
};
cExcpetionRetry.Retry();
writer.LogMessage = "Beginning FTP...";
cExcpetionRetry.Retry();
Ftp ftpMain = new Ftp()
{
AppendIfExists = bAppendIfExists,
Archive = bArchiveAfterFTP,
ArchiveFileWithTimestamp = bArchiveFileWithTimestamp,
DateAdded = bDateAdded,
ConnectionString = writer.ConnectionString,
LocalDirectory = strLocalDirectory,
RemoteDirectory = strRemoteDirectory,
LocalFile = strLocalFile,
RemoteFile = strWildCard,
FileName = strFileName,
JobName = strJobName,
Server = strServer,
Port = nPort,
UserName = strUserName,
Password = strPassword,
FtpCommand = strFtpCommand,
FtpType = strFtpType,
WildCard = strWildCard,
EncryptionKey = strEncryptionKey,
Debug = true
};
cExcpetionRetry.Function = ftpMain.DoFtp;
cExcpetionRetry.Retry();
writer.LogMessage = "FTP Complete.";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
if (ftpMain.FileCount > 0)
{
if (bArchiveAfterFTP && strArchiveDirectory.Length > 0 && Directory.Exists(strArchiveDirectory))
{
writer.LogMessage = "Archiving files...";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
ftpMain.ArchiveDirectory = strArchiveDirectory;
ftpMain.ArchiveRetainDays = nArchiveRetainDays;
ftpMain.MaximumArchiveCopies = nMaxArchiveCount;
cExcpetionRetry.Function = ftpMain.ArchiveFiles;
cExcpetionRetry.Retry();
writer.LogMessage = "Archive complete.";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
}
List<string> lstNewFileNames = new List<string>();
if ((bAddDateToFileName || strFileName.Contains("(period)")) && !strFtpCommand.ToUpper().Contains("PUT"))
{
ftpMain.AddDateToFileName = bAddDateToFileName;
ftpMain.LocalDirectory = strLocalDirectory;
ftpMain.DateFormat = strDateFormat;
cExcpetionRetry.Function = ftpMain.AddDateToFile;
cExcpetionRetry.Retry();
lstNewFileNames = ftpMain.NewFileNames;
}
if (bCopyFile && strCopyFileDestination.Length != 0 && Directory.Exists(strCopyFileDestination))
{
string[] FilesToCopy = Directory.GetFiles(strLocalDirectory, strWildCard);
if (FilesToCopy.Length > 0)
{
writer.LogMessage = "Copying files to " + strCopyFileDestination;
writer.WriteLog();
foreach (string strCopyFile in FilesToCopy)
{
string strCopyName = strCopyFile.Substring(strCopyFile.LastIndexOf("\\") + 1);
if (File.Exists(Path.Combine(strCopyFileDestination, strCopyName)))
{
writer.LogMessage = "Deleting duplicate file (" + strCopyName + ")";
writer.WriteLog();
File.Delete(Path.Combine(strCopyFileDestination, strCopyName));
writer.LogMessage = "File " + Path.Combine(strCopyFileDestination, strCopyName) + " was deleted.";
writer.WriteLog();
}
File.Copy(strCopyFile, Path.Combine(strCopyFileDestination, strCopyName));
writer.LogMessage = strCopyFile + " copied to " + strCopyFileDestination;
writer.WriteLog();
}
}
}
if (bMoveFilesAfterImport && strFileDestination.Length != 0)
{
ftpMain.MoveFilesAfterImport = bMoveFilesAfterImport;
ftpMain.FileDestination = strFileDestination;
cExcpetionRetry.Function = ftpMain.MoveFiles;
cExcpetionRetry.Retry();
writer.LogMessage = "Move complete";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
if (bArchiveAfterFTP)
{
if (strLocalDirectory != strFileDestination)
{
string[] arrFilesToDelete = Directory.GetFiles(strLocalDirectory, strWildCard);
int nFilesToDelete = arrFilesToDelete.Length;
if (nFilesToDelete > 0)
{
writer.LogMessage = nFilesToDelete == 1 ? "Deleting one file..." : "Deleting " + nFilesToDelete.ToString() + " files...";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
if (arrFilesToDelete.Length > 0)
{
foreach (string strDelete in arrFilesToDelete)
{
if (File.Exists(strDelete))
{
File.Delete(strDelete);
}
}
}
writer.LogMessage = "Delete complete.";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
}
else
{
writer.LogMessage = "No files to delete. Skipping...";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
}
}
}
}
if (bDeleteAfterFTP)
{
if (ftpMain.FileCount > 0)
{
cExcpetionRetry.Function = ftpMain.DeleteFiles;
cExcpetionRetry.Retry();
}
else
{
writer.LogMessage = "No files to delete. Skipping...";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
}
}
}
writer.LogMessage = "Session closed.";
cExcpetionRetry.Function = writer.WriteLog;
cExcpetionRetry.Retry();
requiredVariables.Unlock();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Variables exceptionVariables = null;
ExceptionWriter ew = new ExceptionWriter()
{
Exception = ex,
JobName = strJobName,
ConnectionString = Dts.Connections[0].ConnectionString
};
ew.WriteException();
Dts.VariableDispenser.LockOneForWrite("User::StackTrace", ref exceptionVariables);
exceptionVariables["User::StackTrace"].Value = ex.StackTrace.Replace("\r\n", " ");
Dts.VariableDispenser.LockOneForWrite("User::Target", ref exceptionVariables);
exceptionVariables["User::Target"].Value = ex.TargetSite.Name;
Dts.VariableDispenser.LockOneForWrite("User::InnerException", ref exceptionVariables);
exceptionVariables["User::InnerException"].Value = ex.InnerException == null ? "" : ex.InnerException.StackTrace.Replace("\r\n", " ");
Dts.VariableDispenser.LockOneForWrite("User::HelpLink", ref exceptionVariables);
exceptionVariables["User::HelpLink"].Value = ex.HelpLink ?? "";
exceptionVariables.Unlock();
Dts.TaskResult = (int)ScriptResults.Failure;
}