Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload a file to an SFTP server in C# (.NET)? [closed]

Tags:

c#

.net

sftp

Does a free .NET library exist with which I can upload a file to a SFTP (SSH FTP) server, which throws exceptions on problems with the upload and allows the monitoring of its progress?

like image 438
Corey Avatar asked Sep 17 '08 19:09

Corey


People also ask

How SFTP works step by step?

When a client system requests a file transfer, SFTP creates a secure connection between the client and the SFTP server. This connection usually goes through port 22. SFTP then uses the SSH protocol to encrypt the requested file and transfer it to the client.


2 Answers

Maybe you can script/control winscp?

Update: winscp now has a .NET library available as a nuget package that supports SFTP, SCP, and FTPS

like image 94
Kasprzol Avatar answered Sep 22 '22 15:09

Kasprzol


Following code shows how to upload a file to a SFTP server using our Rebex SFTP component.

// create client, connect and log in  Sftp client = new Sftp(); client.Connect(hostname); client.Login(username, password);  // upload the 'test.zip' file to the current directory at the server  client.PutFile(@"c:\data\test.zip", "test.zip");  client.Disconnect(); 

You can write a complete communication log to a file using a LogWriter property as follows. Examples output (from FTP component but the SFTP output is similar) can be found here.

client.LogWriter = new Rebex.FileLogWriter(    @"c:\temp\log.txt", Rebex.LogLevel.Debug);  

or intercept the communication using events as follows:

Sftp client = new Sftp(); client.CommandSent += new SftpCommandSentEventHandler(client_CommandSent); client.ResponseRead += new SftpResponseReadEventHandler(client_ResponseRead); client.Connect("sftp.example.org");  //...  private void client_CommandSent(object sender, SftpCommandSentEventArgs e) {     Console.WriteLine("Command: {0}", e.Command); }  private void client_ResponseRead(object sender, SftpResponseReadEventArgs e) {     Console.WriteLine("Response: {0}", e.Response); } 

For more info see tutorial or download a trial and check samples.

like image 28
Martin Vobr Avatar answered Sep 22 '22 15:09

Martin Vobr