Skip to content

Instantly share code, notes, and snippets.

@EstebanGit215
Forked from DavidDeSloovere/upload.cs
Created March 3, 2020 16:22
Show Gist options
  • Select an option

  • Save EstebanGit215/19eee9663fbff9817b95911b9c95d575 to your computer and use it in GitHub Desktop.

Select an option

Save EstebanGit215/19eee9663fbff9817b95911b9c95d575 to your computer and use it in GitHub Desktop.
SFTP upload with SSH.NET
const string host = "domainna.me";
const string username = "chucknorris";
const string password = "norrischuck";
const string workingdirectory = "/highway/hell";
const string uploadfile = @"c:\yourfilegoeshere.txt";
Console.WriteLine("Creating client and connecting");
using (var client = new SftpClient(host, port, username, password))
{
client.Connect();
Console.WriteLine("Connected to {0}", host);
client.ChangeDirectory(workingdirectory);
Console.WriteLine("Changed directory to {0}", workingdirectory);
var listDirectory = client.ListDirectory(workingdirectory);
Console.WriteLine("Listing directory:");
foreach (var fi in listDirectory)
{
Console.WriteLine(" - " + fi.Name);
}
using (var fileStream = new FileStream(uploadfile, FileMode.Open))
{
Console.WriteLine("Uploading {0} ({1:N0} bytes)", uploadfile, fileStream.Length);
client.BufferSize = 4 * 1024; // bypass Payload error large files
client.UploadFile(fileStream, Path.GetFileName(uploadfile));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment