I recently had to upload files to my server using sftp as well. To my surprise, you can actually use the System.Net .net libraries to do this. System.net contains FtpWebRequest and FtpWebResponse classes which prove to be very helpful.
Uploading might look like this:
public void upload(string remoteFile, string localFile, string host, string user, string pass)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpStream = ftpRequest.GetRequestStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Open);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
and can be used like this:
upload("/public_html/testUpload.txt", @"C:\Users\1161074\Documents\testUpload.txt", @"ftp://xx.xxx.xxx.xx/", "username", "password");
I suggest making a little class out of this to have upload/download, etc. Thats what I did. Let me know if you would like it.