I am receiving error "The requested operation failed". This code is being successfully used by another development group in our company but I am not able to get it to work. The code is connecting to the ftp site, just can upload.
public static void UploadSFTP(string fullFilePath, string remoteFilePath, string host, int port, string username, string password)
public static void UploadSFTP(string fullFilePath, string remoteFilePath, string host, int port, string username, string password)
{
Logger.LogInfo("Starting UploadSFTP");
try
{
string localDirectoryPath = Path.GetDirectoryName(fullFilePath);
DirectoryInfo di = new DirectoryInfo(localDirectoryPath);
// The output directory should already exist.
if (!Directory.Exists(localDirectoryPath))
{
throw new DirectoryNotFoundException(string.Format(" The specified directory does not exist. directory={0}", localDirectoryPath));
}
ConnectionInfo connectionInfo = new PasswordConnectionInfo(host, username, password);
using (SftpClient sftp = new SftpClient(connectionInfo))
{
Logger.LogInfo(string.Format(" Connecting via SFTP to host={0}, port={1}", host, port));
sftp.Connect();
Logger.LogInfo(" Attempting to upload " + fullFilePath);
using (Stream reader = File.OpenRead(fullFilePath))
{
sftp.UploadFile(reader, Path.Combine(remoteFilePath, Path.GetFileName(fullFilePath)));
}
Logger.LogInfo(" Successfully uploaded.");
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
Logger.LogInfo("Ending UploadSFTP");
}