sftp/Test.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | using sfFTPLib; using System; using System.Collections.Generic; using System.Text; namespace CSharp { internal class Test { // fields private sfFTPLib.SSHConnection _connection = null ; private sfFTPLib.SFTPConnection _sftp = null ; // methods public void Start() { var global = new sfFTPLib.Global(); // If LoadLicense is not called a trial license is automatically obtained from the activation server. The FTP Library uses WinHTTP to access // the activation server at www.smartftp.com (TLS, port 443). Ensure that your application is not blocked by any firewall. // TODO: insert the provided serial after the purchase of a license //global.LoadLicense("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"); // Create our COM object through the Interop (Early Binding) _connection = new sfFTPLib.SSHConnection(); // host settings _connection.Host = "localhost" ; _connection.Port = 22; _connection.Username = "username" ; _connection.Password = "password" ; var fileLogger = _connection.SetFileLogger(); fileLogger.file = "ssh.log" ; // Limit authentications var authentications = new SSHAuthentication[2]; authentications[0] = sfFTPLib.SSHAuthentication.ftpSSHAuthenticationPassword; authentications[1] = sfFTPLib.SSHAuthentication.ftpSSHAuthenticationNone; object objAuthentications = (System.Array)authentications; //_connection.set_Authentications(ref objAuthentications); // This snippet shows how to read the array System.Array arrayAuthentications = (System.Array)_connection.get_Authentications(); for ( int i = 0; i < arrayAuthentications.Length; i++) { arrayAuthentications.GetValue(i); } // load private key //sfFTPLib.KeyManager keymanager = new sfFTPLib.KeyManager(); // From file //_connection.PrivateKey = keymanager.LoadFile("Identity", "password"); // // From current users's certificate store // The thumbprint can be obtained from the certificate properties: // 1. Start certmgr.msc // 2. Go to the Personal\Certificates store // 3. Double click the certificate // 4. Go to the Details tab // 5. Select the Thumbprint field //_connection.PrivateKey = keymanager.LoadFromCertificateStore("dc3c1e61ac08b37e14a1025145e5c63bac2b7d05"); // connect to host System.Console.WriteLine( "Connecting to \"{0}\"." , _connection.Host); _connection.Connect(); SFTPTest(); _connection.Disconnect(); } public void SFTPTest() { _sftp = _connection.CreateSFTPConnection(); var fileLogger = _sftp.SetFileLogger(); fileLogger.file = "sftp.log" ; _sftp.Connect(); var homeDirectory = _sftp.RealPath( "." ); var items = _sftp.ReadDirectory(homeDirectory); foreach (sfFTPLib.FTPItem item in items) { var line = string .Format( "Type={0}; Name={1};" , item.Type, item.Name); if (item.Type == sfFTPLib.ItemType.ftpItemTypeRegularFile) { line += string .Format( " Size={0}" , item.Size); } System.Console.WriteLine(line); } // download file // Note: No resume in this sample ulong startPosition = 0; ulong endPosition = ulong .MaxValue; // Use the absolute path to the source file _sftp.DownloadFileEx( "/License.txt" , "License.txt" , sfFTPLib.DataTransferType.ftpDataTransferTypeImage, startPosition, endPosition, ( int )sfFTPLib.DownloadFlags.ftpDownloadFlagReadBeyondEnd, null ); System.Console.WriteLine( "DownloadFile() successful." ); System.Console.WriteLine( "LastTransferBytes = {0} B" , _sftp.LastTransferBytes); System.Console.WriteLine( "LastTransferTime = {0} s" , _sftp.LastTransferTime); System.Console.WriteLine( "LastTransferSpeed = {0} B/s" , _sftp.LastTransferSpeed); // upload file //_sftp.UploadFileEx(@"c:\test.dat", @"/remotefolder/test.dat", sfFTPLib.DataTransferType.ftpDataTransferTypeImage, startPosition, null) } } } |