using Renci.SshNet;
using System.IO;
namespace YourProjectNamespace
{
class sftp
{
public static void UploadSFTPFile(string host, string username,
string password, string sourcefile, string destinationpath, int port)
{
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
client.ChangeDirectory(destinationpath);
using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, Path.GetFileName(sourcefile));
}
}
}
}
}
- เรียกใช้ sftp class จาก form Code (C#)
using System;
using System.IO;
using sftp;
string source = @"FilePath and FileName of Local File to Upload";
string destination = @"SFTP Server File Destination Folder";
string host = "SFTP Host";
string username = "User Name";
string password = "password";
int port = 22; //Port 22 is defaulted for SFTP upload
sftp.UploadSFTPFile(host, username, password, source, destination, port);