Does this capability come with .Net? If not, what's the best free library? I need something that will throw good exceptions when there is a problem, and allows me to monitor its progress.
There is no solution for this within the .net framework.
http://www.eldos.com/sbb/sftpcompare.php outlines a list of un-free options.
your best free bet is to extend SSH using Granados. http://www.routrek.co.jp/en/product/varaterm/granados.html
Unfortunately, it's not in the .NET Framework itself. My wish is that you could integrate with FileZilla, but I don't think it exposes an interface. They do have scripting I think, but it won't be as clean obviously.
I've used CuteFTP in a project which does SFTP. It exposes a COM component which I created a .NET wrapper around. The catch, you'll find, is permissions. It runs beautifully under the Windows credentials which installed CuteFTP, but running under other credentials requires permissions to be set in DCOM.
You may want to take a look a SharpSSH. It supports SFTP out of the box and it's OpenSource.
For another un-free option try edtFTPnet/PRO. It has comprehensive support for SFTP, and also supports FTPS (and of course FTP) if required.
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.
Though not free, it is definitely the best investment I ever made for my company besides buying Visual Studio. You can take a look at chilkat's implementation below: