While Debugging I found that the Exception in the Yellow marked line Below, I have Created SFTP connection in my local server and SFTP Connection is Connected via fileZilla, May I know Whether it is Coding mistake or SFTP Setup Mistake.
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using Renci.SshNet;
using System.Text;
namespace CIT_SFTPConnnect
{
public class ConnectorClass
{
public static string uploadSFTPFile(string host,string username,string password,System.IO.Stream sourceFile,string destinationPath,int port,string fileName,string privateKeyFilePath = "")
{
string successStr = "Fail";
List<AuthenticationMethod> methods;
/*It depends if the private key file is present for authentication. If the SFTP is key secured then the private key file has to be passed.*/
if (privateKeyFilePath != "")
{
var privateKeyFile = new PrivateKeyFile(privateKeyFilePath);
methods = new List<AuthenticationMethod>
{
new PasswordAuthenticationMethod(username, password),
new PrivateKeyAuthenticationMethod(username, privateKeyFile)
};
}
else
{
methods = new List<AuthenticationMethod>
{
new PasswordAuthenticationMethod(username, password)
};
}
try
{
var connectionInfo = new ConnectionInfo(host, port, username, methods.ToArray());
using (SftpClient sftpclient = new SftpClient(connectionInfo))
{
sftpclient.Connect();
sftpclient.ChangeDirectory(destinationPath.Trim());
sourceFile.Position = 0;
sftpclient.BufferSize = 8 * 1024;
sftpclient.UploadFile(sourceFile, fileName);
}
successStr = "Pass";
}
catch (WebException e)
{
successStr = "Fail"+e;
}
return successStr;
}
}
}