0% found this document useful (0 votes)
66 views

SFTP Upload With

This document discusses uploading files via SFTP using SSH.NET in C#. It provides code to connect to an SFTP server, change directories, list files, and upload a file from the local C:\ drive. It notes that the library only allows uploading files from C:\ and subdirectories, not other drives like D:, and asks how to upload files from D:.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

SFTP Upload With

This document discusses uploading files via SFTP using SSH.NET in C#. It provides code to connect to an SFTP server, change directories, list files, and upload a file from the local C:\ drive. It notes that the library only allows uploading files from C:\ and subdirectories, not other drives like D:, and asks how to upload files from D:.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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));
}
}

==============

Missing declare the variable "port"

====================

We are trying to upload files in D:\ drive but, this library only allows to upload
files in C:\ and its subdirectories. We have tried, ChangeDirectory() to change the
root directory to D:\ which fails with the error - "No such file".
Any thought would be appreciated, on how can we achieve it?

You might also like