If you find this project interesting, go to Home and click the star in the box to the right. When you click it, it should turn green, if not - it looks like this:
So here you are looking for documentation. Unfortunately, this project is formally in beta, and we do not have the time to always update the sample code to reflect new changes. Or add samle code for new features for that matter. But here I'm going to show you some code samples of the typical uses of this library.
If for any reason a sample does not work with a release, make sure you have the latest release, or go to Source Code and you can download the absolute latest code. No binaries though.
NOTE: These samples will be based of our unit tests. You can find them under Source Code -> Renci.SshClient -> Renci.SshNet.Tests.
/// <summary> /// This sample will list the contents of the current directory. /// </summary> public void ListDirectory() { string host = ""; string username = ""; string password = ""; string remoteDirectory = "."; // . always refers to the current directory. using (var sftp = new SftpClient(host, username, password)) { var files = sftp.ListDirectory(remoteDirectory); foreach (var file in files) { Console.WriteLine(file.FullName); } } }
/// <summary> /// This sample will upload a file on your local machine to the remote system. /// </summary> public void UploadFile() { string host = ""; string username = ""; string password = ""; string localFileName = ""; string remoteFileName = System.IO.Path.GetFileName(localFile); using (var sftp = new SftpClient(host, username, password)) { sftp.Connect(); using (var file = File.OpenRead(localFileName)) { sftp.UploadFile(remoteFileName, file); } sftp.Disconnect(); } }
/// <summary> /// This sample will download a file on the remote system to your local machine. /// </summary> public void DownloadFile() { string host = ""; string username = ""; string password = ""; string localFileName = System.IO.Path.GetFileName(localFile); string remoteFileName = ""; using (var sftp = new SftpClient(host, username, password)) { sftp.Connect(); using (var file = File.OpenWrite(localFileName)) { sftp.DownloadFile(remoteFileName, file); } sftp.Disconnect(); } }
/// <summary> /// This sample will open a port on your local machine, and allow connections to be forwarded /// to the specified remote host:port. /// </summary> public void LocalPortForward() { string host = ""; string username = ""; string password = ""; int localPort = 8084; string hostToForwardTo = "www.google.com"; int portToForwardTo = 80; using (var client = new SshClient(host, username, password)) { client.Connect(); var tunnel = client.AddForwardedPort<ForwardedPortLocal>("localhost", localPort, hostToForwardTo, portToForwardTo); tunnel.Exception += new EventHandler<ExceptionEventArgs>(this.OnTunnelError); tunnel.Start(); // You can now connect to localhost on port set in localPort. System.Console.ReadKey(); } } /// <summary> /// This method will be called when an error has occurred. /// </summary> /// <param name="sender">Typicall it's the SshClient instance.</param> /// <param name="e">Information about the exception.</param> public void OnTunnelError(object sender, ExceptionEventArgs e) { // An error must be handled. System.Diagnostics.Debugger.Break(); }