Aniket Narvankar

Aniket Narvankar

  • 566
  • 2.1k
  • 603.2k

Upload File on SFTP Server Using Asp.Net

Mar 4 2019 7:06 AM
I want to upload File on SFTP Server,first I need to check wether there is a folder with specific name  on SFTP Server,if folder exists i will save file in that folder,if not I will create it and then save it.
 
Following is the code I tried
 
using (SftpClient client = new SftpClient(host,port,username,password))
{
string path = @"export\home\MEDREPORT_UHC\UAT\";
client.Connect();
if (!client.Exists(path + "Testing"))
{
client.CreateDirectory(path + "Testing");
}
Stream fin = File.OpenRead(filePath);
client.UploadFile(fin, path + "\\" + "Testing" + "\\" + filename, true);
fin.Close();
client.Disconnect();
}
I want to create a folder name Testing on the path given is export\home\MEDREPORT_UHC\UAT\
i.e. inside UAT folder,but instead of creating folder inside UAT,it created folder with name 
export\home\MEDREPORT_UHC\UAT\Testing and saves file in it.
 
How to do this? Kindly provide solution on this